• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python pypandoc.convert_text函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pypandoc.convert_text函数的典型用法代码示例。如果您正苦于以下问题:Python convert_text函数的具体用法?Python convert_text怎么用?Python convert_text使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了convert_text函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: convert_ipynb_to_gallery

def convert_ipynb_to_gallery(file_name):
    """
    Blatantly stolen + adapted from
    https://gist.github.com/wuhuikai/4a7ceb8bc52454e17a4eb8327d538d85

    """
    python_file = ""

    nb_dict = json.load(open(file_name))
    cells = nb_dict['cells']

    for i, cell in enumerate(cells):
        if i == 0:
            assert cell['cell_type'] == 'markdown', \
                'First cell has to be markdown'

            md_source = ''.join(cell['source'])
            rst_source = pdoc.convert_text(md_source, 'rst', 'md')
            python_file = '"""\n' + rst_source + '\n"""'
        else:
            if cell['cell_type'] == 'markdown':
                md_source = ''.join(cell['source'])
                rst_source = pdoc.convert_text(md_source, 'rst', 'md')
                commented_source = '\n'.join([
                    '# ' + x for x in rst_source.split('\n')
                ])
                python_file = python_file + '\n\n\n' + '#' * 70 + '\n' + \
                    commented_source
            elif cell['cell_type'] == 'code':
                source = ''.join(cell['source'])
                python_file = python_file + '\n' * 2 + source

    open(file_name.replace('.ipynb', '.py'), 'w').write(python_file)
开发者ID:tamasgal,项目名称:km3pipe,代码行数:33,代码来源:nb2sphx.py


示例2: convert_ipynb_to_gallery

def convert_ipynb_to_gallery(file_name):
    python_file = ""

    nb_dict = json.load(open(file_name))
    cells = nb_dict['cells']

    for i, cell in enumerate(cells):
        if i == 0:
            assert cell['cell_type'] == 'markdown', \
                'First cell has to be markdown'

            md_source = ''.join(cell['source'])
            rst_source = pdoc.convert_text(md_source, 'rst', 'md')
            python_file = '"""\n' + rst_source + '\n"""'
        else:
            if cell['cell_type'] == 'markdown':
                md_source = ''.join(cell['source'])
                rst_source = pdoc.convert_text(md_source, 'rst', 'md')
                commented_source = '\n'.join(['# ' + x for x in
                                              rst_source.split('\n')])
                python_file = python_file + '\n\n\n' + '#' * 70 + '\n' + \
                    commented_source
            elif cell['cell_type'] == 'code':
                source = ''.join(cell['source'])
                python_file = python_file + '\n' * 2 + source

    open(file_name.replace('.ipynb', '.py'), 'w').write(python_file)
开发者ID:maestrotf,项目名称:pymepps,代码行数:27,代码来源:convert_gallery.py


示例3: expand_description

 def expand_description(self, exp):
     return {
         "general": pypandoc.convert_text(exp.find("./description/general").text, "latex", format="md"),
         "details": [
             pypandoc.convert_text(detail.text.strip(), "latex", format="md")
             for detail in exp.findall("./description/details/detail")
         ],
     }
开发者ID:benjaminran,项目名称:resume,代码行数:8,代码来源:content.py


示例4: main

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"])
开发者ID:monome,项目名称:teletype,代码行数:53,代码来源:docs.py


示例5: render_to_format

def render_to_format(request, format, title, template_src, context):

    # for some weird reason we have to cast here explicitly
    format = str(format)
    title = str(title)

    if format in 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':
                args = ['-V', 'geometry:margin=1in']
                content_disposition = 'filename=%s.%s' % (title, format)
            else:
                args = []
                content_disposition = 'attachment; filename=%s.%s' % (title, format)

            print (content_disposition)

            # 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

        return response
    else:
        return HttpResponseBadRequest(_('This format is not supported.'))
开发者ID:rdmorganiser,项目名称:rdmo,代码行数:51,代码来源:utils.py


示例6: ChangeSpellDesc2MD

def ChangeSpellDesc2MD():
    with open(json_file['spells']) as json_data:
        spells = json.load(json_data)

        for spell in spells:
            #print(spell)
            spell['desc'] = pypandoc.convert_text(spell['desc'],'md',format='html',extra_args=['--wrap=none'])
            if 'higher_level' in spell:
                spell['higher_level'] = pypandoc.convert_text(spell['higher_level'],'md',format='html',extra_args=['--wrap=none'])
            if 'material' in spell:
                spell['material'] = pypandoc.convert_text(spell['material'],'md',format='html',extra_args=['--wrap=none'])
        
        with open(json_file['spells'], 'w') as outfile:
            json.dump(spells, outfile)
开发者ID:snan,项目名称:open5e,代码行数:14,代码来源:parse_content.py


示例7: parse

 def parse(self, response):
     talk_ids = collections.defaultdict(list)
     for day in response.css('div.schedule__day.iframe_schedule_day'):
         curr_date = day.css('p.schedule__date::text').get()
         for r in day.css('div::attr(data-link)'):
             talk_ids[r.get()] = curr_date
     yield talk_ids
     for talk in response.css('div.details.uv-card__mask'):
         for session in talk.css('div.uv-card--session'):
             time_of_day = session.css(
                 'span.session__time:nth-child(1)').xpath(
                 'normalize-space()').get()
             talk_id = talk.xpath('@id').get()
             desc = session.css('div.safe-description').get()
             try:
                 desc_md = html2text(desc)
                 desc = pypandoc.convert_text(desc_md, 'rst', format='md')
             except:
                 pass
             yield {'title': session.xpath('string(.//h2)').get(),
                    'datetime': dateparser.parse('{date} {year} {tod}'.format(
                         date=talk_ids[talk_id],
                         year=2016,
                         tod=time_of_day)),
                    'description': desc,
                    'spearkers': session.css('''
                         div.session__speakers-box
                             div.uv-shortcard__title::text''').extract()}
开发者ID:redapple,项目名称:pyvideo-contrib,代码行数:28,代码来源:talks.py


示例8: md2rst

def md2rst(comment):
    """Convert a comment from protobuf markdown to restructuredtext.

    This method:
    - Replaces proto links with literals (e.g. [Foo][bar.baz.Foo] -> `Foo`)
    - Resolves relative URLs to https://cloud.google.com
    - Runs pandoc to convert from markdown to restructuredtext
    """
    comment = _replace_proto_link(comment)
    comment = _replace_relative_link(comment)
    # Calling pypandoc.convert_text is slow, so we try to avoid it if there are
    # no special characters in the markdown.
    if any([i in comment for i in '`[]*_']):
        comment = pypandoc.convert_text(comment, 'rst', format='commonmark')
        # Comments are now valid restructuredtext, but there is a problem. They
        # are being inserted back into a descriptor set, and there is an
        # expectation that each line of a comment will begin with a space, to
        # separate it from the '//' that begins the comment. You would think
        # that we could ignore this detail, but it will cause formatting
        # problems down the line in gapic-generator because parsing code will
        # try to remove the leading space, affecting the indentation of lines
        # that actually do begin with a space, so we insert the additional
        # space now. Comments that are not processed by pypandoc will already
        # have a leading space, so should not be changed.
        comment = _insert_spaces(comment)
    return comment
开发者ID:googleapis,项目名称:artman,代码行数:26,代码来源:descriptor_set_tasks.py


示例9: render_markdown

def render_markdown(value):
    """Render Markdown"""
    try:
        output = pypandoc.convert_text(value, to='html5', format='md', extra_args=['--mathjax'])
    except RuntimeError:
        output = value
    return output
开发者ID:gwgundersen,项目名称:glossary,代码行数:7,代码来源:__init__.py


示例10: convert

    def convert(self, text):
        text = '\n\n'.join([re.sub(self.regexCodeBlock, r'<pre>\1</pre>', block) for block in text.split('\n\n')])

        # convert from textile to markdown
        text = pypandoc.convert_text(text, 'markdown_strict', format='textile')

        # pandoc does not convert everything, notably the [[link|text]] syntax
        # is not handled. So let's fix that.

        # [[ wikipage | link_text ]] -> [link_text](wikipage)
        text = re.sub(self.regexWikiLinkWithText, self.wiki_link, text, re.MULTILINE | re.DOTALL)

        # [[ link_url ]] -> [link_url](link_url)
        text = re.sub(self.regexWikiLinkWithoutText, self.wiki_link, text, re.MULTILINE | re.DOTALL)

        # nested lists, fix at least the common issues
        text = text.replace("    \\#\\*", "    -")
        text = text.replace("    \\*\\#", "    1.")

        # Redmine is using '>' for blockquote, which is not textile
        text = text.replace("&gt; ", ">")

        # wiki note macros
        text = re.sub(self.regexTipMacro, r'---\n**TIP**: \1\n---\n', text, re.MULTILINE | re.DOTALL)
        text = re.sub(self.regexNoteMacro, r'---\n**NOTE**: \1\n---\n', text, re.MULTILINE | re.DOTALL)
        text = re.sub(self.regexWarningMacro, r'---\n**WARNING**: \1\n---\n', text, re.MULTILINE | re.DOTALL)
        text = re.sub(self.regexImportantMacro, r'---\n**IMPORTANT**: \1\n---\n', text, re.MULTILINE | re.DOTALL)

        # all other macros
        text = re.sub(self.regexAnyMacro, r'\1', text, re.MULTILINE | re.DOTALL)

        return text
开发者ID:tamaloa,项目名称:redmine-gitlab-migrator,代码行数:32,代码来源:wiki.py


示例11: tokenize_block

def tokenize_block(source: str, pandoc_extra_args: list=None) -> list:
    """
    Convert a Jupyter output to Pandoc's JSON AST.
    """
    if pandoc_extra_args is None:
        pandoc_extra_args = []
    json_doc = pypandoc.convert_text(source, to='json', format='markdown', extra_args=pandoc_extra_args)
    return json.loads(json_doc)['blocks']
开发者ID:TomAugspurger,项目名称:stitch,代码行数:8,代码来源:stitch.py


示例12: text_decode

def text_decode(text):
	if re.search(r'\\u', text):
		body = fix_arnaud_post(text)
	elif is_html(text):
		text = escape_special_characters(text)
		body = pypandoc.convert_text(text, 'markdown_strict', format='html')
	else:
		body = text
	return body
开发者ID:5CORNERS,项目名称:www.le-francais.ru,代码行数:9,代码来源:fix_old_forum_posts.py


示例13: save_url

def save_url(chapter, title, url):
    file_name = '{}.tex'.format(title.replace('/', '\\').replace(':', ' -'))
    path = pathlib.Path(os.path.join('content', chapter, 'images'))
    path.mkdir(parents=True, exist_ok=True)

    p = mercury.parse(url)
    html = save_images(p.content, path)

    content = pypandoc.convert_text(html, 'tex', format='html')
    write_content(path.parent.joinpath(file_name), content)
开发者ID:phuong3030,项目名称:milewski-ctfp-pdf,代码行数:10,代码来源:scraper.py


示例14: create

 def create(self, variables, md_output, pdf_output):
     env = Environment(loader=PackageLoader('qanta', 'reporting/templates'))
     template = env.get_template(self.template)
     markdown = template.render(variables)
     if md_output is not None:
         with open(md_output, 'w') as f:
             f.write(markdown)
     try:
         import pypandoc
         pypandoc.convert_text(
             markdown,
             'pdf',
             format='md',
             outputfile=pdf_output,
             extra_args=['-V', 'geometry:margin=.75in']
         )
     except Exception as e:
         log.warn('Pandoc was not installed or there was an error calling it, omitting PDF report')
         log.warn(str(e))
开发者ID:Pinafore,项目名称:qb,代码行数:19,代码来源:report_generator.py


示例15: main

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":
            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")])
        elif ext == ".pdf" or ext == ".tex":
            latex = Path(TEMPLATE_DIR / "latex_preamble.md").read_text()
            latex += output
            pypandoc.convert_text(
                latex,
                format=input_format,
                to=ext[1:],
                outputfile=str(p),
                extra_args=["--standalone",
                            "--column=80",
                            "--toc",
                            "--toc-depth=2",
                            "--latex-engine=xelatex",
                            "--variable=papersize:A4"])
开发者ID:bpcmusic,项目名称:teletype,代码行数:42,代码来源:docs.py


示例16: read_long_description

def read_long_description():
    try:
        import pypandoc
        with open("README.md") as f:
            text = f.read()

        # Remove screenshots as they get rendered poorly on PyPi
        stripped_text = text[:text.index("# Screenshots")].rstrip()
        return pypandoc.convert_text(stripped_text, 'rst', format='md')
    except:
        return ""
开发者ID:NickMinnellaCS96,项目名称:urh,代码行数:11,代码来源:setup.py


示例17: test_basic_pypandoc_example

    def test_basic_pypandoc_example(self):
        """
        This test is testing a basic pypandoc function call.
        """

        pypandoc_result = pypandoc.convert_text(
            '- *foo* bar', 'html5', format='org')
        expected_html5_result = '<ul>\n<li><strong>foo</strong> bar</li>\n</ul>\n'

        self.assertEqual(
            Utils.normalize_lineendings(pypandoc_result),
            Utils.normalize_lineendings(expected_html5_result))
开发者ID:novoid,项目名称:lazyblorg,代码行数:12,代码来源:pypandoc_test.py


示例18: test_pypandoc_with_umlauts

    def test_pypandoc_with_umlauts(self):
        """
        This test is testing umlaut and charset with pypandoc.
        """

        pypandoc_result = pypandoc.convert_text(
            'This is an umlaut test: öÄ߀',
            'html5',
            format='org',
            encoding='utf-8')
        expected_html5_result = '<p>This is an umlaut test: öÄ߀</p>\n'

        # FIXXME: Umlaut conversion does habe encoding issues.
        self.assertEqual(Utils.normalize_lineendings(pypandoc_result),
                         Utils.normalize_lineendings(expected_html5_result))
开发者ID:novoid,项目名称:lazyblorg,代码行数:15,代码来源:pypandoc_test.py


示例19: _init_settings

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']))
开发者ID:SoVictor,项目名称:Lerna,代码行数:48,代码来源:settings.py


示例20: exercise

def exercise(src):
    # import pdb; pdb.set_trace()
    d, p, *_ = html.fragments_fromstring(src)

    # title = d.attrib['data-title']
    title = d.find('h1').text_content().strip().replace("Exercise: ", "")
    question = convert_text(p.text, "latex", format="markdown")
    tpl = dedent('''\

    ---

    \\begin{{Exercise}}[title={{{title}}}]
    {question}
    \\end{{Exercise}}
    ''').format(title=title, question=question)

    return tpl
开发者ID:kreegerlabs,项目名称:python,代码行数:17,代码来源:exporter.py



注:本文中的pypandoc.convert_text函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pypar.barrier函数代码示例发布时间:2022-05-27
下一篇:
Python pypandoc.convert_file函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap