93 if content and not tidyied_content.strip(): |
93 if content and not tidyied_content.strip(): |
94 raise Exception('Content could not be tidyfied') |
94 raise Exception('Content could not be tidyfied') |
95 return str(tidyied_content).decode('utf8') |
95 return str(tidyied_content).decode('utf8') |
96 |
96 |
97 |
97 |
98 def get_filetemp(mode="r"): |
98 def get_filetemp(mode="r", suffix=''): |
99 (fd, fname) = mkstemp() |
99 (fd, fname) = mkstemp(suffix) |
100 return (os.fdopen(fd, mode), fname) |
100 return (os.fdopen(fd, mode), fname) |
101 |
|
102 # build absolute address for latex header file |
|
103 _tmp_ = __file__.split(os.path.sep)[:-1] |
|
104 _tmp_.append('latex_header.txt') |
|
105 _tmp_.insert(0, os.path.sep) |
|
106 |
|
107 LATEX_HEADER_PATH = os.path.join(*_tmp_) |
|
108 |
|
109 if not os.path.isfile(LATEX_HEADER_PATH): |
|
110 raise Exception('LATEX_HEADER_PATH is not a file!') |
|
111 |
101 |
112 @dj_memoize |
102 @dj_memoize |
113 def pandoc_markdown2pdf(content=None, file_name=None): |
103 def pandoc_markdown2pdf(content=None, file_name=None): |
114 """ |
104 """ |
115 Convert markdown content to pdf |
105 Convert markdown content to pdf |
117 >>> pdf_content = pandoc_markdown2pdf('# dssd') |
107 >>> pdf_content = pandoc_markdown2pdf('# dssd') |
118 """ |
108 """ |
119 content = content_or_file_name(content, file_name) |
109 content = content_or_file_name(content, file_name) |
120 |
110 |
121 # write file to disk |
111 # write file to disk |
122 temp_file, input_temp_name = get_filetemp('w') |
112 temp_file, input_temp_name = get_filetemp('w', 'input') |
123 fp_error, error_temp_name = get_filetemp('w') |
113 fp_error, error_temp_name = get_filetemp('w', 'err') |
124 |
114 |
125 temp_file.write(content.encode(_PANDOC_ENCODING)) |
115 temp_file.write(content.encode(_PANDOC_ENCODING)) |
126 temp_file.close() |
116 temp_file.close() |
127 |
117 |
128 # custom latex header |
118 cust_tex = " --xetex " |
129 cust_head_tex = " --custom-header=%s " %LATEX_HEADER_PATH |
|
130 |
119 |
131 # use markdown2pdf |
120 # use markdown2pdf |
132 retcode = call(MARKDOWN2PDF_BIN + cust_head_tex + ' ' + input_temp_name, shell=True, stderr=fp_error) |
121 retcode = call(MARKDOWN2PDF_BIN + cust_tex + ' ' + input_temp_name, shell=True, stderr=fp_error) |
133 fp_error.close() |
122 fp_error.close() |
134 |
123 |
135 fp_error = file(error_temp_name) |
124 fp_error = file(error_temp_name) |
136 error = fp_error.read() |
125 error = fp_error.read() |
137 fp_error.close() |
126 fp_error.close() |
177 raise Exception("Output format [%s] is not a supported format [%s]" % (to_format, ' '.join(OUTPUT_FORMATS))) |
166 raise Exception("Output format [%s] is not a supported format [%s]" % (to_format, ' '.join(OUTPUT_FORMATS))) |
178 if type(content) != unicode: |
167 if type(content) != unicode: |
179 raise Exception('Content is not in unicode format!') |
168 raise Exception('Content is not in unicode format!') |
180 |
169 |
181 # temp file |
170 # temp file |
182 input_file, input_temp_name = get_filetemp('w') |
171 input_file, input_temp_name = get_filetemp('w', 'input') |
183 output_temp_fp, output_temp_name = get_filetemp() |
172 output_temp_fp, output_temp_name = get_filetemp('r', 'output') |
184 output_temp_fp.close() |
173 output_temp_fp.close() |
185 |
174 |
186 error_temp_fp, error_temp_name = get_filetemp('w') |
175 error_temp_fp, error_temp_name = get_filetemp('w', 'err') |
187 error_temp_fp.close() |
176 error_temp_fp.close() |
188 |
177 |
189 input_file.write(content.encode(_PANDOC_ENCODING)) |
178 input_file.write(content.encode(_PANDOC_ENCODING)) |
190 input_file.close() |
179 input_file.close() |
191 |
180 |