28
28
29
29
from __future__ import unicode_literals
30
30
31
- import codecs
31
+ import io
32
32
import os
33
- import re
33
+
34
+ from nikola .plugin_categories import PageCompiler
35
+ from nikola .utils import makedirs , req_missing , write_metadata
34
36
35
37
try :
36
38
import misaka
45
47
gist_extension = None
46
48
podcast_extension = None
47
49
48
- from nikola .plugin_categories import PageCompiler
49
- from nikola .utils import makedirs , req_missing , write_metadata
50
-
51
50
52
51
class CompileMisaka (PageCompiler ):
53
52
"""Compile Misaka into HTML."""
54
-
55
53
name = "misaka"
56
54
demote_headers = True
57
55
@@ -66,21 +64,32 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
66
64
if misaka is None :
67
65
req_missing (['misaka' ], 'build this site (compile with misaka)' )
68
66
makedirs (os .path .dirname (dest ))
69
- with codecs .open (dest , "w+" , "utf8" ) as out_file :
70
- with codecs .open (source , "r" , "utf8" ) as in_file :
67
+ with io .open (dest , "w+" , encoding = "utf8" ) as out_file :
68
+ with io .open (source , "r" , encoding = "utf8" ) as in_file :
71
69
data = in_file .read ()
72
- if not is_two_file :
73
- data = re .split ('(\n \n |\r \n \r \n )' , data , maxsplit = 1 )[- 1 ]
74
- output = misaka .html (data , extensions = self .ext )
75
- output , shortcode_deps = self .site .apply_shortcodes (output , filename = source , with_dependencies = True , extra_context = dict (post = post ))
76
- out_file .write (output )
77
- if post is None :
78
- if shortcode_deps :
79
- self .logger .error (
80
- "Cannot save dependencies for post {0} (post unknown)" ,
81
- source )
70
+ output , error_level , deps , shortcode_deps = self .compile_string (data , source , is_two_file , post , lang )
71
+ out_file .write (output )
72
+ if post is None :
73
+ if deps :
74
+ self .logger .error (
75
+ "Cannot save dependencies for post {0} (post unknown)" ,
76
+ source )
77
+ else :
78
+ post ._depfile [dest ] += shortcode_deps
79
+ if error_level == 0 :
80
+ return True
82
81
else :
83
- post ._depfile [dest ] += shortcode_deps
82
+ return False
83
+
84
+ def compile_string (self , data , source_path = None , is_two_file = True , post = None , lang = None ):
85
+ """Compile markdown into HTML strings."""
86
+ if not is_two_file :
87
+ _ , data = self .split_metadata (data , post , lang )
88
+ from nikola import shortcodes as sc
89
+ new_data , shortcodes = sc .extract_shortcodes (data )
90
+ output = misaka .html (new_data , extensions = self .ext )
91
+ output , shortcode_deps = self .site .apply_shortcodes_uuid (output , shortcodes , filename = source_path , extra_context = {'post' : post })
92
+ return output , 0 , [], shortcode_deps
84
93
85
94
def compile_html (self , source , dest , is_two_file = True ):
86
95
"""Compile the post into HTML (deprecated API)."""
@@ -101,9 +110,9 @@ def create_post(self, path, **kw):
101
110
makedirs (os .path .dirname (path ))
102
111
if not content .endswith ('\n ' ):
103
112
content += '\n '
104
- with codecs .open (path , "wb +" , "utf8" ) as fd :
113
+ with io .open (path , "w +" , encoding = "utf8" ) as fd :
105
114
if onefile :
106
- fd .write ('<!-- \n ' )
107
- fd .write (write_metadata (metadata ))
108
- fd .write ('-->\n \n ' )
115
+ fd .write ('<!--\n ' )
116
+ fd .write (write_metadata (metadata ). strip () )
117
+ fd .write ('\n -->\n \n ' )
109
118
fd .write (content )
0 commit comments