34
34
35
35
"""
36
36
37
- import codecs
37
+ import io
38
38
import os
39
- from os .path import abspath
40
39
import subprocess
40
+ import tempfile
41
41
42
42
from nikola .plugin_categories import PageCompiler
43
43
from nikola .utils import makedirs , write_metadata
@@ -49,28 +49,39 @@ class CompileKramdown(PageCompiler):
49
49
name = "kramdown"
50
50
demote_headers = True
51
51
52
+ def compile_string (self , data , source_path = None , is_two_file = True , post = None , lang = None ):
53
+ """Compile markdown into HTML strings."""
54
+ if not is_two_file :
55
+ _ , data = self .split_metadata (data , post , lang )
56
+
57
+ from nikola import shortcodes as sc
58
+ new_data , shortcodes = sc .extract_shortcodes (data )
59
+
60
+ # Kramdown takes a file as argument and prints to stdout
61
+ with tempfile .NamedTemporaryFile (mode = 'w+' , delete = False ) as source :
62
+ source .write (new_data )
63
+ with tempfile .NamedTemporaryFile (mode = 'w+' , delete = False ) as dest :
64
+ command = ['kramdown' , '-o' , 'html' , '--no-auto-ids' , source .name ]
65
+ subprocess .check_call (command , stdout = dest )
66
+ with open (dest .name , 'r' ) as inf :
67
+ output = inf .read ()
68
+
69
+ os .unlink (source .name )
70
+ os .unlink (dest .name )
71
+ output , shortcode_deps = self .site .apply_shortcodes_uuid (output , shortcodes , filename = source_path , extra_context = {'post' : post })
72
+
73
+ return output , shortcode_deps
74
+
52
75
def compile (self , source , dest , is_two_file = True , post = None , lang = None ):
53
76
"""Compile the source file into HTML and save as dest."""
54
77
makedirs (os .path .dirname (dest ))
55
- command = ['kramdown' , '-o' , 'html' , '--no-auto-ids' , abspath (source )]
56
-
57
- # Windows kludge
58
- if os .name == 'nt' :
59
- command [4 ] = command [4 ].replace ("\\ " , "\\ \\ " )
60
-
61
- with open (abspath (dest ), 'wt' ) as f :
62
- subprocess .check_call (command , stdout = f )
63
- with open (dest , 'r' , encoding = 'utf-8' ) as inf :
64
- output , shortcode_deps = self .site .apply_shortcodes (inf .read ())
65
- with open (dest , 'w' , encoding = 'utf-8' ) as outf :
66
- outf .write (output )
67
- if post is None :
68
- if shortcode_deps :
69
- self .logger .error (
70
- "Cannot save dependencies for post {0} (post unknown)" ,
71
- source )
72
- else :
73
- post ._depfile [dest ] += shortcode_deps
78
+ with io .open (dest , "w+" , encoding = "utf8" ) as out_file :
79
+ with io .open (source , "r" , encoding = "utf8" ) as in_file :
80
+ data = in_file .read ()
81
+ output , shortcode_deps = self .compile_string (data , source , is_two_file , post , lang )
82
+ out_file .write (output )
83
+ post ._depfile [dest ] += shortcode_deps
84
+ return True
74
85
75
86
def create_post (self , path , content = None , onefile = False , is_page = False , ** kw ):
76
87
"""Create post file with optional metadata."""
@@ -80,9 +91,9 @@ def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
80
91
makedirs (os .path .dirname (path ))
81
92
if not content .endswith ('\n ' ):
82
93
content += '\n '
83
- with codecs .open (path , "wb +" , "utf8" ) as fd :
94
+ with io .open (path , "w +" , encoding = "utf8" ) as fd :
84
95
if onefile :
85
96
fd .write ("<!--\n " )
86
- fd .write (write_metadata (metadata ))
87
- fd .write ("-->\n \n " )
97
+ fd .write (write_metadata (metadata ). strip () )
98
+ fd .write ("\n -->\n \n " )
88
99
fd .write (content )
0 commit comments