def has_pandoc(): # pragma: no cover try: import pypandoc pypandoc.get_pandoc_version() return True except ImportError: logger.debug("pypandoc is not installed.") except FileNotFoundError: logger.debug("pandoc is not installed.") return False
def has_pandoc(): # pragma: no cover try: with captured_output(): import pypandoc pypandoc.get_pandoc_version() return True except (OSError, ImportError): logger.info("pypandoc is not installed.") except FileNotFoundError: logger.info("pandoc is not installed.") return False
def _init_settings(): import yaml def adjust_path(loader, node): return os.path.join(BASE_DIR, loader.construct_scalar(node)) yaml.add_constructor('!path', adjust_path) configuration_files = ('settings.yml', 'static/settings.yml', 'local_settings.yml') for filename in configuration_files: with open(os.path.join(BASE_DIR, 'lerna', filename), encoding='utf-8-sig') as f: for yml_key, yml_data in yaml.load(f).items(): if yml_key == 'PREPEND': for key, value in yml_data.items(): globals()[key] = value + globals()[key] elif yml_key == 'APPEND': for key, value in yml_data.items(): globals()[key] += value elif yml_key == 'OVERRIDE': for cnf_name, sub_data in yml_data.items(): cnf = globals()[cnf_name] for key, value in sub_data.items(): cnf[key] = value else: globals()[yml_key] = yml_data # TODO: Log every failure. try: import pypandoc as pd except ImportError: pass else: try: pd.get_pandoc_version() except OSError: pass else: output = pd.convert_text('', 'html', format='latex') if output not in ('', '\n'): raise Exception('pandoc is found, but has not passed a sample test (%r)' % output) def check_filter(f): try: pd.convert_text('', 'html', format='latex', filters=[f]) return True except RuntimeError: return False PANDOC['REQUIRED'] = True PANDOC['FILTERS'] = list(filter(check_filter, PANDOC['FILTERS']))
def test_get_pandoc_version(self): assert "HOME" in os.environ, "No HOME set, this will error..." version = pypandoc.get_pandoc_version() self.assertTrue(isinstance(version, pypandoc.string_types)) major = int(version.split(".")[0]) # according to http://pandoc.org/releases.html there were only two versions 0.x ... self.assertTrue(major in [0, 1])
def renderer(self, text): """ Renders a flat page to HTML. :param text: the text of the flat page :type text: string """ if type(text) == str: text = unicode(text, self.app.config["FLATPAGES_ENCODING"]) if self.pre_render: text = render_template_string(Markup(text)) extra_args = [ "--filter=pandoc-crossref", "--filter=pandoc-citeproc", "--filter=pandoc-sidenote", "--standalone", "--mathml", "--base-header-level=2", "--highlight-style", "pygments", '--bibliography="pages/all.bib"', "--csl=pages/lncs.csl", "-Mreference-section-title=References", "-Mlink-citations=true" ] pandocver = int(pypandoc.get_pandoc_version()[0]) if pandocver < 2: extra_args.append("-S") format_str = "markdown+raw_tex+yaml_metadata_block" else: format_str = "markdown+raw_tex+smart+yaml_metadata_block" output = pypandoc.convert_text(text.encode("utf8"), 'html', format=format_str, extra_args=extra_args) return output
def test_get_pandoc_version(self): assert "HOME" in os.environ, "No HOME set, this will error..." version = pypandoc.get_pandoc_version() self.assertTrue(isinstance(version, pypandoc.string_types)) major = int(version.split(".")[0]) # according to http://pandoc.org/releases.html there were only two versions 0.x ... self.assertTrue(major in [0, 1, 2])
def main(): home_link = "https://raw.githubusercontent.com/mbadry1/DeepLearning.ai-Summary/master/" marks_down_links = { "Deeplearning.ai summary Homepage": home_link + "Readme.md", "01- Neural Networks and Deep Learning": home_link + "1-%20Neural%20Networks%20and%20Deep%20Learning/Readme.md", "02- Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization": home_link + "2-%20Improving%20Deep%20Neural%20Networks/Readme.md", "03- Structuring Machine Learning Projects": home_link + "3-%20Structuring%20Machine%20Learning%20Projects/Readme.md", "04- Convolutional Neural Networks": home_link + "4-%20Convolutional%20Neural%20Networks/Readme.md", "05- Sequence Models": home_link + "5-%20Sequence%20Models/Readme.md", } # Extracting pandoc version print("pandoc_version:", pypandoc.get_pandoc_version()) print("pandoc_path:", pypandoc.get_pandoc_path()) print("\n") # Starting downloading and converting for key, value in marks_down_links.items(): print("Converting", key) pypandoc.convert_file( value, 'pdf', extra_args=['--latex-engine=xelatex', '-V', 'geometry:margin=1.5cm'], outputfile=(key + ".pdf") ) print("Converting", key, "completed")
def pandocify(self): try: import pypandoc # pypandoc spews install instructions to stderr when it can't find # pandoc. So we redirect stderr and restore it with a saner # message. old_stderr = sys.stderr pandoc_found = True try: with io.StringIO() as s: sys.stderr = s pypandoc.get_pandoc_version() except OSError: pandoc_found = False finally: sys.stderr = old_stderr if not pandoc_found: print( '*****************************************************\n' 'Pandoc man page conversion failed, skipping man pages\n' '*****************************************************\n', file=sys.stderr) return None # now do the actual conversion here = '.' mandir = os.path.join(here, 'man') man_pages = [] for f in os.listdir(mandir): if f.endswith('.md'): path = os.path.join(mandir, f) outfile = f'{path[:-3]}.1' pypandoc.convert_file(path, 'man', outputfile=outfile, extra_args=['-s']) man_pages.append(outfile) except ModuleNotFoundError: print( '*********************************************\n' 'Module pypandoc not found, skipping man pages\n' '*********************************************\n', file=sys.stderr) return None return man_pages
def download_pandoc(): """Download pandoc if not already installed""" try: # Check whether it is already installed pypandoc.get_pandoc_version() except OSError: # Pandoc not installed. Let's download it. pypandoc.download_pandoc() # Hack to delete the downloaded file from the folder, # otherwise it could get accidently committed to the repo # by other scripts in the repo. pf = platform if pf.startswith('linux'): pf = 'linux' url = pypandoc.pandoc_download._get_pandoc_urls()[0][pf] filename = url.split('/')[-1] os.remove(filename)
def _has_pypandoc(): """Check if pypandoc package available.""" try: import pypandoc # noqa # Import error raised only when function called version = pypandoc.get_pandoc_version() except (ImportError, OSError): return None, None else: return True, version
def main(): if len(sys.argv) <= 1: sys.exit("Please supply a filename") input_format = "markdown" pdf_output = common_md() html_output = pdf_output["html"] pdf_output = pdf_output["pdf"] print() for arg in sys.argv[1:]: p = Path(arg).resolve() print(f"Generating: {p}") ext = p.suffix if ext == ".md": p.write_text(pdf_output) elif ext == ".html": html_output = "# " + VERSION_STR + "\n\n" + html_output pypandoc.convert_text( html_output, format=input_format, to="html5", outputfile=str(p), extra_args=["--standalone", "--self-contained", "--toc", "--toc-depth=2", "--css=" + str(TEMPLATE_DIR / "docs.css"), "--template=" + str(TEMPLATE_DIR / "template.html")]) elif ext == ".pdf" or ext == ".tex": latex_preamble = env.get_template("latex_preamble.jinja2.md") latex = latex_preamble \ .render(title=VERSION_STR, fonts_dir=FONTS_DIR) + "\n\n" latex += pdf_output pandoc_version = int(pypandoc.get_pandoc_version()[0]) engine = ("--pdf-engine=xelatex" if pandoc_version >= 2 else "--latex-engine=xelatex") pypandoc.convert_text( latex, format=input_format, to=ext[1:], outputfile=str(p), extra_args=["--standalone", "--column=80", "--toc", "--toc-depth=2", engine, "--variable=papersize:A4"])
def __init__(self, local_repo_path): self.repo_path = local_repo_path self.repo = Repo(local_repo_path) # make sure we use at least version 17 of pandoc # TODO: fix this test, it will not work properly for version 1.2 or 1.100 version = pypandoc.get_pandoc_version() if (version < "1.17"): log.error('You need at least pandoc 1.17.0, download from http://pandoc.org/installing.html') exit(1) self.textile_converter = TextileConverter()
def __init__(self, local_repo_path, textile_converter): self.repo_path = local_repo_path self.repo = Repo(local_repo_path) # make sure we use at least version 17 of pandoc # TODO: fix this test, it will not work properly for version 1.2 or 1.100 version = pypandoc.get_pandoc_version() if (version < "1.17"): log.error('You need at least pandoc 1.17.0, download from http://pandoc.org/installing.html') exit(1) self.textile_converter = textile_converter
def main(): if len(sys.argv) <= 1: sys.exit("Please supply a filename") input_format = "markdown" output = common_md() print() for arg in sys.argv[1:]: p = Path(arg).resolve() print(f"Generating: {p}") ext = p.suffix if ext == ".md": p.write_text(output) elif ext == ".html": output = "# " + VERSION_STR + "\n\n" + output pypandoc.convert_text( output, format=input_format, to="html5", outputfile=str(p), extra_args=["--standalone", "--self-contained", "--toc", "--toc-depth=2", "--css=" + str(TEMPLATE_DIR / "docs.css"), "--template=" + str(TEMPLATE_DIR / "template.html5")]) elif ext == ".pdf" or ext == ".tex": latex_preamble = env.get_template("latex_preamble.jinja2.md") latex = latex_preamble \ .render(title=VERSION_STR, fonts_dir=FONTS_DIR) + "\n\n" latex += output pandoc_version = int(pypandoc.get_pandoc_version()[0]) engine = ("--pdf-engine=xelatex" if pandoc_version >= 2 else "--latex-engine=xelatex") pypandoc.convert_text( latex, format=input_format, to=ext[1:], outputfile=str(p), extra_args=["--standalone", "--column=80", "--toc", "--toc-depth=2", engine, "--variable=papersize:A4"])
def render_to_format(request, format, title, template_src, context): if format in dict(settings.EXPORT_FORMATS): # render the template to a html string template = get_template(template_src) html = template.render(context) # remove empty lines html = os.linesep.join([line for line in html.splitlines() if line.strip()]) if format == 'html': # create the response object response = HttpResponse(html) else: if format == 'pdf': # check pandoc version (the pdf arg changed to version 2) if pypandoc.get_pandoc_version().split('.')[0] == '1': args = ['-V', 'geometry:margin=1in', '--latex-engine=xelatex'] else: args = ['-V', 'geometry:margin=1in', '--pdf-engine=pdflatex'] content_disposition = 'filename="%s.%s"' % (title, format) else: args = [] content_disposition = 'attachment; filename="%s.%s"' % (title, format) # create a temporary file (tmp_fd, tmp_filename) = mkstemp('.' + format) # convert the file using pandoc pypandoc.convert_text(html, format, format='html', outputfile=tmp_filename, extra_args=args) # read the temporary file file_handler = os.fdopen(tmp_fd, 'rb') file_content = file_handler.read() file_handler.close() # delete the temporary file os.remove(tmp_filename) # create the response object response = HttpResponse(file_content, content_type='application/%s' % format) response['Content-Disposition'] = content_disposition.encode('utf-8') return response else: return HttpResponseBadRequest(_('This format is not supported.'))
def pandoc_version_at_least(required_version): required = [int(x) for x in required_version.split('.')] installed = [int(x) for x in pypandoc.get_pandoc_version().split('.')] for idx, digit in enumerate(installed): try: req = required[idx] except IndexError: return True else: if digit < req: return False if digit > req: return True return True
def check_pandoc_on_startup(): '''On startup, checks if pandoc is installed. If it is, continues to main application. Otherwise, pandoc will be installed.''' print('Looking for pandoc...') try: version = pypandoc.get_pandoc_version() print('Found version {0}'.format(version)) except OSError: print('Not found! Downloading...') pypandoc.pandoc_download.download_pandoc() print('Download complete!')
def markdown_to_reveal(text: str, config: Config) -> str: """ Transform a Markdown input file to an HTML (reveal.js) output string. Parameters ---------- markdown_text Markdown text to convert to HTML. config Markdownreveal configuration. Returns ------- The converted string. """ extra_args = [ '-s', '--slide-level=2', '-V', 'revealjs-url=revealjs', ] if config['katex']: pandoc_version = get_pandoc_version() if LooseVersion(pandoc_version) < LooseVersion('2.0'): extra_args.extend([ '--katex=katex/katex.min.js', '--katex-stylesheet=katex/katex.min.css', ]) else: extra_args.extend([ '--katex=katex/', ]) extra_args.extend(pandoc_extra_to_args(config)) extra_args.extend(reveal_extra_to_args(config)) input_format = 'markdown' if config['emoji_codes']: input_format += '+emoji' output = convert_text( source=text, format=input_format, to='revealjs', extra_args=extra_args, ) # HTML substitution output = tweak_html(output, config) return output
def __init__(self): # make sure we use at least version 17 of pandoc # TODO: fix this test, it will not work properly for version 1.2 or 1.100 version = pypandoc.get_pandoc_version() if (version < "1.17"): log.error('You need at least pandoc 1.17.0, download from http://pandoc.org/installing.html') exit(1) # precompile regular expressions self.regexWikiLinkWithText = re.compile(r'\\\[\\\[\s*([^\]]*?)\s*\|\s*([^\]]*?)\s*\\\]\\\]') self.regexWikiLinkWithoutText = re.compile(r'\\\[\\\[\s*([^\]]*?)\s*\\\]\\\]') self.regexTipMacro = re.compile(r'\{\{tip\((.*?)\)\}\}') self.regexNoteMacro = re.compile(r'\{\{note\((.*?)\)\}\}') self.regexWarningMacro = re.compile(r'\{\{warning\((.*?)\)\}\}') self.regexImportantMacro = re.compile(r'\{\{important\((.*?)\)\}\}') self.regexAnyMacro = re.compile(r'\{\{(.*)\}\}') self.regexCodeBlock = re.compile(r'\A ((.|\n)*)', re.MULTILINE)
def renderer(self, text): """ Renders a flat page to HTML. :param text: the text of the flat page :type text: string """ # if type(text) == str: # text = str(text, self.app.config["FLATPAGES_ENCODING"]) if self.pre_render: text = render_template_string(Markup(text)) extra_args = [ "--filter=pandoc-crossref", "--citeproc", "--filter=pandoc-sidenote", "--standalone", "--mathml", "--shift-heading-level-by=2", "--highlight-style", "pygments", "--bibliography=pages/all.bib", "--csl=pages/lncs.csl", "-Mreference-section-title=References", "-Mlink-citations=true", # putting this template in the default place should work but doesn't... # "--template=/Users/joshfriedlander/.local/share/templates/default.html" # so we add it explicitly here "--template=default.html" ] pandocver = int(pypandoc.get_pandoc_version()[0]) if pandocver < 2: extra_args.append("-S") format_str = "markdown+raw_tex+yaml_metadata_block" else: format_str = "markdown+raw_tex+smart+yaml_metadata_block+header_attributes" output = pypandoc.convert_text(text.encode("utf8"), 'html', format=format_str, extra_args=extra_args) return output
def insert_citation_keys(citation_quads, markdown, csl=False, bib=False): """ Insert citations into the markdown text replacing the old citation keys Args: citation_quads (tuple): a quad tuple of all citation info markdown (str): the markdown text to modify Returns: markdown (str): the modified Markdown """ # Renumber quads if using numbers for citation links grouped_quads = [ list(g) for _, g in groupby(citation_quads, key=lambda x: x[0]) ] for quad_group in grouped_quads: full_citation = quad_group[0][0] # the full citation block replacement_citaton = "".join( ["[^{}]".format(quad[2]) for quad in quad_group]) # if cite_inline is true, convert full_citation with pandoc and add to replacement_citaton if csl and bib: # Verify that the pandoc installation is newer than 2.11 pandoc_version = pypandoc.get_pandoc_version() pandoc_version_tuple = tuple( int(ver) for ver in pandoc_version.split(".")) if pandoc_version_tuple <= (2, 11): raise RuntimeError( f"Your version of pandoc (v{pandoc_version}) is " "incompatible with the cite_inline feature.") inline_citation = _convert_pandoc_citekey(bib, csl, full_citation) replacement_citaton = f" {inline_citation}{replacement_citaton}" # Make sure inline citations doesn't get an extra whitespace by # replacing it with whitespace added first markdown = markdown.replace(f" {full_citation}", replacement_citaton) markdown = markdown.replace(full_citation, replacement_citaton) return markdown
def renderer(self, text): """ Renders a flat page to HTML. :param text: the text of the flat page :type text: string """ #if type(text) == str: # text = str(text, self.app.config["FLATPAGES_ENCODING"]) if self.pre_render: text = render_template_string(Markup(text)) extra_args = [ "--filter=pandoc-crossref", "--filter=pandoc-citeproc", "--filter=pandoc-sidenote", "--standalone", "--mathml", "--base-header-level=2", "--highlight-style", "pygments", "--bibliography=pages/all.bib", "--csl=pages/lncs.csl", "-Mreference-section-title=References", "-Mlink-citations=true" ] pandocver = int(pypandoc.get_pandoc_version()[0]) if pandocver < 2: extra_args.append("-S") format_str = "markdown+raw_tex+yaml_metadata_block" else: format_str = "markdown+raw_tex+smart+yaml_metadata_block" output = pypandoc.convert_text( text.encode("utf8"), 'html', format = format_str, extra_args=extra_args ) return output
def __init__(self, local_repo_path): self.repo_path = local_repo_path self.repo = Repo(local_repo_path) # make sure we use at least version 17 of pandoc # TODO: fix this test, it will not work properly for version 1.2 or 1.100 version = pypandoc.get_pandoc_version() if (version < "1.17"): log.error('You need at least pandoc 1.17.0, download from http://pandoc.org/installing.html') exit(1) # precompile regular expressions self.regexNonASCII = re.compile(r'[^a-zA-Z_0-9]') self.regexWikiLinkWithText = re.compile(r'\\\[\\\[\s*(.*?)\s*\|\s*(.*?)\s*\\\]\\\]') self.regexWikiLinkWithoutText = re.compile(r'\\\[\\\[\s*(.*?)\s*\\\]\\\]') self.regexTipMacro = re.compile(r'\{\{tip\((.*?)\)\}\}') self.regexNoteMacro = re.compile(r'\{\{note\((.*?)\)\}\}') self.regexWarningMacro = re.compile(r'\{\{warning\((.*?)\)\}\}') self.regexImportantMacro = re.compile(r'\{\{important\((.*?)\)\}\}') self.regexAnyMacro = re.compile(r'\{\{(.*)\}\}')
def main(): marks_down_links = { "Standford CS231n 2017 Summary": "https://raw.githubusercontent.com/mbadry1/CS231n-2017-Summary/master/README.md", } # Extracting pandoc version print("pandoc_version:", pypandoc.get_pandoc_version()) print("pandoc_path:", pypandoc.get_pandoc_path()) print("\n") # Starting downloading and converting for key, value in marks_down_links.items(): print("Converting", key) pypandoc.convert_file( value, 'pdf', extra_args=['--pdf-engine=xelatex', '-V', 'geometry:margin=1.5cm'], outputfile=(key + ".pdf")) print("Converting", key, "completed")
def check_pandoc_on_startup(self): '''On startup, checks if pandoc is installed. If it is, continues to main application. Otherwise, prompts the user to install.''' try: version = pypandoc.get_pandoc_version() self.status_bar.showMessage( 'Found pandoc version {0}'.format(version)) except OSError: error_string = '''GrammarGen could not find a local installation of Pandoc. If you have already installed it, check that it is in your PATH. Otherwise, GrammarGen can install it automatically. Proceed with installation?''' download_prompt = QtWidgets.QMessageBox.question( self, 'Pandoc not found', error_string, QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if download_prompt == QtWidgets.QMessageBox.Yes: pypandoc.pandoc_download.download_pandoc() else: sys.exit(0)
def format_pandoc(entries, csl_path): """ Format the entries using pandoc Args: entries (dict): dictionary of entries csl_path (str): path to formatting CSL Fle Returns: references (dict): dictionary of citation texts """ pandoc_version = tuple( int(ver) for ver in pypandoc.get_pandoc_version().split(".")) citations = OrderedDict() for key, entry in entries.items(): bibtex_string = BibliographyData(entries={ entry.key: entry }).to_string("bibtex") if pandoc_version >= (2, 11): citations[key] = _convert_pandoc_new(bibtex_string, csl_path) else: citations[key] = _convert_pandoc_legacy(bibtex_string, csl_path) return citations
def __init__(self, tree=False, textformat='textile'): # make sure we use at least version 17 of pandoc # TODO: fix this test, it will not work properly for version 1.2 or 1.100 version = pypandoc.get_pandoc_version() if (version < "1.17"): log.error( 'You need at least pandoc 1.17.0, download from http://pandoc.org/installing.html' ) exit(1) # precompile regular expressions self.regexWikiLinkWithText = re.compile( r'\\\[\\\[\s*([^\]]*?)\s*\|\s*([^\]]*?)\s*\\\]\\\]') self.regexWikiLinkWithoutText = re.compile( r'\\\[\\\[\s*([^\]]*?)\s*\\\]\\\]') self.regexTipMacro = re.compile(r'\{\{tip\((.*?)\)\}\}') self.regexNoteMacro = re.compile(r'\{\{note\((.*?)\)\}\}') self.regexWarningMacro = re.compile(r'\{\{warning\((.*?)\)\}\}') self.regexImportantMacro = re.compile(r'\{\{important\((.*?)\)\}\}') self.regexAnyMacro = re.compile(r'\{\{(.*)\}\}') self.regexCodeBlock = re.compile(r'\A ((.|\n)*)', re.MULTILINE) self.regexCollapse = re.compile(r'({{collapse\s?\(([^)]+)\))(.*)(}})', re.MULTILINE | re.DOTALL) self.regexParagraph = re.compile(r'p(\(+|(\)+)?>?|=)?\.', re.MULTILINE | re.DOTALL) self.regexCodeHighlight = re.compile( r'(<code\s?(class=\"(.*)\")?>).*(</code>)', re.MULTILINE | re.DOTALL) self.regexAttachment = re.compile( r'attachment:[\'\"“”‘’„”«»](.*)[\'\"“”‘’„”«»]', re.MULTILINE | re.DOTALL) # tree view for wiki pages self.tree = tree # markdown text self.textformat = textformat
def print_pandoc_version() -> None: version = pypandoc.get_pandoc_version() print(f"Using pandoc {version}")
assert set(dirpass) == set(alteredlist) assert prebuild.copydocs('failext', tempsuffix) == 1 def test_listfiles(): """Test listfiles.""" correctlist = [path.realpath('tools/readpass.md')] assert prebuild.listfiles(docdir, mdsuffix, ignore) == correctlist def test_listtable(): """Test listtable.""" assert prebuild.listtable(sampleoutput) == ltable @mark.skipif(get_pandoc_version() < '1.13', reason='requires pandoc >= 1.13') def test_parsedoc(): """Test parsedoc.""" assert prebuild.parsedoc(samplemd) == sampleoutput def test_preparse(): """Test preparse.""" instring = 'A string of <br />markdown.' outstring = 'A string of @br@markdown.' assert prebuild.preparse(instring) == outstring def test_processdocs(): """Test processdocs."""
def render_to_format(request, format, title, template_src, context): if format in dict(settings.EXPORT_FORMATS): # render the template to a html string template = get_template(template_src) html = template.render(context) # remove empty lines html = os.linesep.join( [line for line in html.splitlines() if line.strip()]) if format == 'html': # create the response object response = HttpResponse(html) else: args = [] content_disposition = 'attachment; filename="%s.%s"' % (title, format) if format == 'pdf': # check pandoc version (the pdf arg changed to version 2) if pypandoc.get_pandoc_version().split('.')[0] == '1': args += [ '-V', 'geometry:margin=1in', '--latex-engine=xelatex' ] else: args += [ '-V', 'geometry:margin=1in', '--pdf-engine=xelatex' ] # display pdf in browser content_disposition = 'filename="%s.%s"' % (title, format) elif format == 'rtf': # rtf needs to be standalone args += ['--standalone'] # use reference document for certain file formats refdoc = set_export_reference_document(format, context) if refdoc is not None and (format == 'docx' or format == 'odt'): if pypandoc.get_pandoc_version().startswith("1"): refdoc_param = '--reference-' + format + '=' + refdoc args.extend([refdoc_param]) else: refdoc_param = '--reference-doc=' + refdoc args.extend([refdoc_param]) # create a temporary file (tmp_fd, tmp_filename) = mkstemp('.' + format) log.info("Export " + format + " document using args " + str(args)) # convert the file using pandoc pypandoc.convert_text(html, format, format='html', outputfile=tmp_filename, extra_args=args) # read the temporary file file_handler = os.fdopen(tmp_fd, 'rb') file_content = file_handler.read() file_handler.close() # delete the temporary file os.remove(tmp_filename) # create the response object response = HttpResponse(file_content, content_type='application/%s' % format) response['Content-Disposition'] = content_disposition.encode( 'utf-8') return response else: return HttpResponseBadRequest(_('This format is not supported.'))
from tinydb import Query from archivy import extensions from archivy.models import User from archivy.api import api_bp from archivy.check_changes import run_watcher from archivy.config import Config app = Flask(__name__) app.config.from_object(Config) app.logger.setLevel(logging.INFO) app.register_blueprint(api_bp, url_prefix='/api') # check if pandoc is installed, otherwise install try: pypandoc.get_pandoc_version() except OSError: app.logger.info("Installing pandoc") pypandoc.download_pandoc() # create dir that will hold data if it doesn't already exist DIRNAME = app.config["APP_PATH"] + "/data/" Path(DIRNAME).mkdir(parents=True, exist_ok=True) if app.config["ELASTICSEARCH_ENABLED"]: with app.app_context(): es = extensions.get_elastic_client() try: es.indices.create( index=app.config["INDEX_NAME"], body=app.config["ELASTIC_CONF"])
def get_pandoc_version(): return int(pypandoc.get_pandoc_version().split('.')[0])
def test_get_pandoc_version(self): version = pypandoc.get_pandoc_version() self.assertTrue(isinstance(version, pypandoc.string_types)) major = int(version.split(".")[0]) # according to http://pandoc.org/releases.html there were only two versions 0.x ... self.assertTrue(major in [0, 1])
#!/usr/bin/python3 from os.path import basename, dirname, join as pathjoin, isabs, realpath from os import remove import genanki from bs4 import BeautifulSoup import sys import json import pypandoc import tempfile if len(sys.argv) < 2: print('Usage: ./md2anki.py configEntryName [path to config]') exit(1) print("[i] Using pandoc version %s" % pypandoc.get_pandoc_version()) config_entry = sys.argv[1] path_to_config = "configs.json" relative_path = dirname(sys.argv[0]) this_path = dirname(realpath(__file__)) if len(sys.argv) > 2: relative_path = dirname(sys.argv[2]) path_to_config = basename(sys.argv[2]) input_file, deckname, outputname, model_id, deck_id = None, None, None, None, None pandoc_args = ["-s", "--highlight-style", "tango"] css_files = ["default.css"] css = "" with open(pathjoin(relative_path, path_to_config)) as config_file:
"""Pydocument initialisation.""" from pydocument.document import doc from pydocument.parser import parser from pydocument.templater import templater from pypandoc import get_pandoc_version, pandoc_download if not get_pandoc_version(): pandoc_download() del get_pandoc_version, pandoc_download docx_parser = parser('docx') pdf_parser = parser('pdf')
# URL that lists all pages #url = "http://wiki.libreplan.org/bin/view/LibrePlan/WebTopicList" url = "http://wiki.libreplan-enterprise.com/bin/view/LibrePlan/WebTopicList" # Root directory of old Twiki page root_url = "http://wiki.libreplan-enterprise.com/" # Dir to start exporting to export_root_dir = "./libreplan.wiki/twiki" # All urls we are interested in start with: starts_with = "/twiki/bin/view" # new url location new_url_root = "https://github.com/LibrePlan/libreplan/wiki/twiki" bugcounter = 0 copyright_notice = "<p>\n\nCopyright (c) by the contributing authors. All material on this collaboration platform is the property of the contributing authors.</body></html>" print("Pandoc version: " + pypandoc.get_pandoc_version()) #print(pypandoc.get_pandoc_path()) #print(pypandoc.get_pandoc_formats()) # r = requests.get(url) data = r.text data2 = copy.copy(data) # Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors. mysearch = "<!-- /patternContent-->" if mysearch in data2: data2 = data2[0:data2.index(mysearch)] # </body> </html> data2 = data2 + copyright_notice
def get_pandoc_main_version(): try: return int(pypandoc.get_pandoc_version().split('.')[0]) except OSError: return None
def test_inline_citations(plugin): plugin.config["bib_file"] = os.path.join(test_files_dir, "test.bib") plugin.config["csl_file"] = os.path.join(test_files_dir, "springer-basic-author-date.csl") plugin.config["cite_inline"] = True plugin.on_config(plugin.config) pandoc_version = pypandoc.get_pandoc_version() pandoc_version_tuple = tuple(int(ver) for ver in pandoc_version.split(".")) if pandoc_version_tuple <= (2, 11): pytest.skip( f"Unsupported version of pandoc (v{pandoc_version}) installed.") # Ensure inline citation works quads = [("[@test]", None, "1", None)] test_markdown = 'Hello[@test]' result = "Hello (Author and Author 2019)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Ensure suppressed authors works quads = [("[-@test]", None, "1", None)] test_markdown = 'Suppressed [-@test]' result = "Suppressed (2019)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Ensure affixes work quads = [("[see @test]", None, "1", None)] test_markdown = 'Hello[see @test]' result = "Hello (see Author and Author 2019)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) quads = [("[@test, p. 123]", None, "1", None)] test_markdown = '[@test, p. 123]' result = " (Author and Author 2019, p. 123)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Combined quads = [("[see @test, p. 123]", None, "1", None)] test_markdown = 'Hello[see @test, p. 123]' result = "Hello (see Author and Author 2019, p. 123)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Combined, suppressed author quads = [("[see -@test, p. 123]", None, "1", None)] test_markdown = 'Suppressed [see -@test, p. 123]' result = "Suppressed (see 2019, p. 123)[^1]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Ensure multi references work quads = [("[@test; @Bivort2016]", None, "1", None), ("[@test; @Bivort2016]", None, "2", None)] test_markdown = '[@test; @Bivort2016]' # CSL defines the order, this ordering is therefore expected with springer.csl result = " (De Bivort and Van Swinderen 2016; Author and Author 2019)[^1][^2]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) quads = [("[@test, p. 12; @Bivort2016, p. 15]", None, "1", None), ("[@test, p. 12; @Bivort2016, p. 15]", None, "2", None)] test_markdown = '[@test, p. 12; @Bivort2016, p. 15]' # CSL defines the order, this ordering is therefore expected with springer.csl result = " (De Bivort and Van Swinderen 2016, p. 15; Author and Author 2019, p. 12)[^1][^2]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")) # Ensure multiple inline references works quads = [("[@test]", None, "1", None), ("[see @Bivort2016, p. 123]", None, "2", None)] test_markdown = 'Hello[@test] World [see @Bivort2016, p. 123]' result = "Hello (Author and Author 2019)[^1] World (see De Bivort and Van Swinderen 2016, p. 123)[^2]" assert result == insert_citation_keys(quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex"))
#!/usr/bin/python3 # -*- coding: utf-8 -*- import pypandoc import os print("Pandoc", pypandoc.get_pandoc_version()) base = "../../Note/" for r, ds, fs in os.walk(base): for f in fs: if f.endswith(".md"): src = r + "/" + f dst = src.replace(".md", ".pdf") print(src, "->", dst) print( pypandoc.convert_file(src, "pdf", outputfile=dst, format="gfm", encoding="utf-8", extra_args=[ "-V", "CJKmainfont=Microsoft YaHei", "--pdf-engine=xelatex" ]))