def make_regular_pages(pages_struct, subdir): '''Main function to create regular pages.''' for page_group in pages_struct: # (info-prints) print("Page: ", os.path.join(subdir, page_group[0])) #print('Preprocessing:', page_group[0]) #for subcontent in page_group[1:]: # print(' Subcontent:', subcontent) #print('Page group:', page_group) # Preprocess page main_page_body_subst, plugin_blocks, plugin_blocks_pdf, main_page_tb_vals=preprocess_page_group(subdir, page_group) # (scan doctype) body_doctype=scan_doctype(main_page_body_subst) # Preprocess math content if config.PROCESS_MATH: main_page_body_subst_m = handle_math(subdir, page_group[0], main_page_body_subst) else: main_page_body_subst_m = main_page_body_subst # Generate the menus # # main menu # (info-prints) #print('Generate main menu.') #print('Subdir:', subdir, 'Page group [0]:', page_group[0]) main_menu=generate_main_menu(subdir, page_group[0]) # section menu # (info-prints) #print('Generate section menu.') section_menu_list=generate_section_menu(subdir, page_group[0]) # Finalize this page # prepare for final output final_opts, out_filepath=prepare_final(subdir, page_group, main_page_tb_vals, main_menu, section_menu_list, body_doctype) # (debug-print) #print("main page body subst: ", main_page_body_subst) # final pandoc processing final_html_subst=pandoc_pipe(main_page_body_subst_m, final_opts) # (debug-print) #print("final html subst: ", final_html_subst) # back substitute if plugin_blocks != []: final_html=back_substitute(final_html_subst, plugin_blocks) else: final_html=final_html_subst # write out # (info-print) print('Writing:', out_filepath) write_out(final_html, out_filepath) # PDF production if config.PRODUCE_PDF: # (info-print) print("Produce a PDF.") # (plugins must provide blocks for pdf's) generate_pdf(subdir, page_group[0], main_page_body_subst, plugin_blocks_pdf, main_page_tb_vals)
def generate_pdf(subdir, filename_md, page_body_subst, plugin_blocks_pdf, title_block_vals): '''Main PDF generator function.''' # working directory # needs to be the CONTENT_DIR to include images atm., # (--> could evtl. be improved) # # the directory is cleaned up and the resulting PDF # is _moved_ to PUBLISH_DIR below, wd = os.path.join(config.CONTENT_DIR, subdir) filename_pdf = filename_md.split('.')[0]+'.pdf' # back substitute the plugin blocks into the page body if plugin_blocks_pdf != []: # (debug-print) #print("plugin blocks pdf: ", plugin_blocks_pdf) page_body = back_substitute(page_body_subst, plugin_blocks_pdf) else: page_body = page_body_subst # create pandoc options for title block values opts = [] for index, tb_value in enumerate(title_block_vals): opts.append('--variable='+config.REGULAR_TB_LINES[index]+':'+tb_value) # (debug-print) #print("page body (for pdf):", page_body) # set working directory to the respective content subdir cwd = os.getcwd() os.chdir(wd) # call pandoc pandoc_command = ['pandoc', '-o', filename_pdf] args = pandoc_command+opts proc = subprocess.Popen(args, stdin=subprocess.PIPE) input = page_body.encode() stdout, stderr = proc.communicate(input=input) #subprocess.call(args) # cleanup the subdir from temporary files files = os.listdir('.') tmpfiles = [] for file in files: if '-eps-converted-to.pdf' in file: tmpfiles.append(file) for tmpfile in tmpfiles: os.remove(tmpfile) os.chdir(cwd) # move the pdf to the publish directory inpath = os.path.join(wd, filename_pdf) outdir = os.path.join(config.PUBLISH_DIR, subdir) # (using copy + remove since shutil.move doesn't overwrite) shutil.copy(inpath, outdir) os.remove(inpath)