26
26
27
27
"""Implementation of compile_html based on Mistune."""
28
28
29
- from __future__ import unicode_literals
30
-
31
- import codecs
29
+ import io
32
30
import os
33
- import re
31
+
32
+ from nikola .plugin_categories import PageCompiler
33
+ from nikola .utils import makedirs , req_missing , write_metadata
34
34
35
35
try :
36
36
import mistune
41
41
except ImportError :
42
42
OrderedDict = dict # NOQA
43
43
44
- from nikola .plugin_categories import PageCompiler
45
- from nikola .utils import makedirs , req_missing , write_metadata
46
-
47
44
48
45
class CompileMistune (PageCompiler ):
49
46
"""Compile Markdown into HTML using Mistune."""
@@ -56,26 +53,37 @@ def __init__(self, *args, **kwargs):
56
53
if mistune is not None :
57
54
self .parser = mistune .Markdown ()
58
55
56
+ def compile_string (self , data , source_path = None , is_two_file = True , post = None , lang = None ):
57
+ """Compile markdown into HTML strings."""
58
+ if not is_two_file :
59
+ _ , data = self .split_metadata (data , post , lang )
60
+ from nikola import shortcodes as sc
61
+ new_data , shortcodes = sc .extract_shortcodes (data )
62
+ output = self .parser (new_data )
63
+ output , shortcode_deps = self .site .apply_shortcodes_uuid (output , shortcodes , filename = source_path , extra_context = {'post' : post })
64
+ return output , 0 , [], shortcode_deps
65
+
59
66
def compile (self , source , dest , is_two_file = True , post = None , lang = None ):
60
67
"""Compile the source file into HTML and save as dest."""
61
68
if mistune is None :
62
69
req_missing (['mistune' ], 'build this site (compile with mistune)' )
63
70
makedirs (os .path .dirname (dest ))
64
- with codecs .open (dest , "w+" , "utf8" ) as out_file :
65
- with codecs .open (source , "r" , "utf8" ) as in_file :
71
+ with io .open (dest , "w+" , encoding = "utf8" ) as out_file :
72
+ with io .open (source , "r" , encoding = "utf8" ) as in_file :
66
73
data = in_file .read ()
67
- if not is_two_file :
68
- data = re .split ('(\n \n |\r \n \r \n )' , data , maxsplit = 1 )[- 1 ]
69
- output = self .parser (data )
70
- output , shortcode_deps = self .site .apply_shortcodes (output , filename = source , with_dependencies = True , extra_context = dict (post = post ))
71
- out_file .write (output )
72
- if post is None :
73
- if shortcode_deps :
74
- self .logger .error (
75
- "Cannot save dependencies for post {0} (post unknown)" ,
76
- source )
74
+ output , error_level , deps , shortcode_deps = self .compile_string (data , source , is_two_file , post , lang )
75
+ out_file .write (output )
76
+ if post is None :
77
+ if deps :
78
+ self .logger .error (
79
+ "Cannot save dependencies for post {0} (post unknown)" ,
80
+ source )
81
+ else :
82
+ post ._depfile [dest ] += shortcode_deps
83
+ if error_level == 0 :
84
+ return True
77
85
else :
78
- post . _depfile [ dest ] += shortcode_deps
86
+ return False
79
87
80
88
def compile_html (self , source , dest , is_two_file = True ):
81
89
"""Compile the post into HTML (deprecated API)."""
@@ -96,9 +104,9 @@ def create_post(self, path, **kw):
96
104
makedirs (os .path .dirname (path ))
97
105
if not content .endswith ('\n ' ):
98
106
content += '\n '
99
- with codecs .open (path , "wb +" , "utf8" ) as fd :
107
+ with io .open (path , "w +" , encoding = "utf8" ) as fd :
100
108
if onefile :
101
- fd .write ('<!-- \n ' )
102
- fd .write (write_metadata (metadata ))
103
- fd .write ('-->\n \n ' )
109
+ fd .write ('<!--\n ' )
110
+ fd .write (write_metadata (metadata ). strip () )
111
+ fd .write ('\n -->\n \n ' )
104
112
fd .write (content )
0 commit comments