Python源码示例:pypandoc.convert()
示例1
def getLongDescription():
"""Provides the long description"""
try:
import pypandoc
converted = pypandoc.convert('README.md', 'rst').splitlines()
no_travis = [line for line in converted if 'travis-ci.org' not in line]
long_description = '\n'.join(no_travis)
# Pypi index does not like this link
long_description = long_description.replace('|Build Status|', '')
except Exception as exc:
print('pypandoc package is not installed: the markdown '
'README.md convertion to rst failed: ' + str(exc), file=sys.stderr)
# pandoc is not installed, fallback to using raw contents
with io.open('README.md', encoding='utf-8') as f:
long_description = f.read()
return long_description
示例2
def parse_description(markdown=True):
"""
Parse the description in the README file
"""
if markdown: return parse_markdown()
try:
from pypandoc import convert
readme_file = f'{PACKAGE_ROOT}/docs/index.rst'
if not path.exists(readme_file):
raise ImportError
return convert(readme_file, 'rst')
except ImportError:
return parse_markdown()
示例3
def long_description():
"""Reads and returns the contents of the README.
On failure, returns the project long description.
Returns:
The project's long description.
"""
cwd = os.path.abspath(os.path.dirname(__file__))
readme_path = os.path.join(cwd, 'README.md')
if not os.path.exists(readme_path):
return pylink.__long_description__
try:
import pypandoc
return pypandoc.convert(readme_path, 'rst')
except (IOError, ImportError):
pass
return open(readme_path, 'r').read()
示例4
def long_description(*paths):
'''Returns a RST formated string.
'''
result = ''
# attempt to import pandoc
try:
import pypandoc
except (ImportError, OSError) as e:
print("Unable to import pypandoc - %s" % e)
return result
# attempt md -> rst conversion
try:
for path in paths:
result += '\n' + pypandoc.convert(
path, 'rst', format='markdown'
)
except (OSError, IOError) as e:
print("Failed to convert with pypandoc - %s" % e)
return result
return result
示例5
def convert_to_html(filename):
# Do the conversion with pandoc
output = pypandoc.convert(filename, 'html')
# Clean up with tidy...
output, errors = tidy_document(output, options={
'numeric-entities': 1,
'wrap': 80,
})
print(errors)
# replace smart quotes.
output = output.replace(u"\u2018", '‘').replace(u"\u2019", '’')
output = output.replace(u"\u201c", "“").replace(u"\u201d", "”")
# write the output
filename, ext = os.path.splitext(filename)
filename = "{0}.html".format(filename)
with open(filename, 'w') as f:
# Python 2 "fix". If this isn't a string, encode it.
if type(output) is not str:
output = output.encode('utf-8')
f.write(output)
print("Done! Output written to: {}\n".format(filename))
示例6
def read_md(f):
return convert(f, 'rst')
示例7
def readme():
# I really prefer Markdown to reStructuredText. PyPi does not. This allows me
# to have things how I'd like, but not throw complaints when people are trying
# to install the package and they don't have pypandoc or the README in the
# right place.
# From https://coderwall.com/p/qawuyq/use-markdown-readme-s-in-python-modules
try:
import pypandoc
long_description = pypandoc.convert('README.md', 'rst')
except (IOError, ImportError):
with open('README.md') as f:
return f.read()
else:
return long_description
示例8
def read_md(fpath):
return convert(fpath, 'rst')
示例9
def get_readme():
try:
import pypandoc
readme_data = pypandoc.convert('README.md', 'rst')
except(IOError, ImportError):
readme_data = open('README.md').read()
return readme_data
示例10
def read_markdown(filename):
path = os.path.join(os.path.dirname(__file__), filename)
if not os.path.exists(path):
if 'sdist' in sys.argv:
print('WARNING: did not find %r' % filename, file=sys.stderro)
return
try:
import pypandoc
except ImportError:
if 'sdist' in sys.argv:
print('WARNING: Could not import pypandoc to convert README.md to RST!',
file=sys.stderr)
return open(path).read()
return pypandoc.convert(path, 'rst')
示例11
def readme():
with open('README.md') as f:
content = f.read()
try:
# noinspection PyUnresolvedReferences
from pypandoc import convert
return convert(content, 'rst', 'md')
except ImportError:
print("warning: pypandoc module not found, could not convert Markdown to RST")
return content
示例12
def desc_converter(desc):
desc = pypandoc.convert(desc, to='rst', format='markdown')
return re.sub(r'\[STRIKEOUT:(\:.*)\:``(.*)``\]', r'\1:`\2`', desc).rstrip().replace(r'"', "'")
示例13
def _read_long_description():
try:
import pypandoc
return pypandoc.convert('README.md', 'rst', format='markdown')
except Exception:
return None
示例14
def md2rst(obj):
if WITH_PANDOC:
return pypandoc.convert(obj, to='rst', format='markdown')
else:
return obj.replace('```', '\n')
示例15
def convert_md():
with open(RST_README_PATH, 'wb') as readme:
converted = convert(MD_README_PATH, 'rst')
readme.write(converted.encode('utf-8'))
示例16
def convert_file(src_file, target_folder='.'):
file_name = os.path.basename(src_file)
new_file_name = os.path.splitext(file_name)[0] + '.rst'
new_file_path = os.path.sep.join([target_folder, new_file_name])
print("**** convert: " + src_file + " to " + new_file_path)
if six.PY3:
open(new_file_path, "w").write(pypandoc.convert(src_file, 'rst'))
else:
open(new_file_path, "w").write(pypandoc.convert(src_file, 'rst').encode('utf8'))
示例17
def html2latex(text):
output = pypandoc.convert(text, 'latex', format='html', extra_args=['-f', 'html+tex_math_dollars'])
return output
示例18
def __str__(self):
from pypandoc import convert
return str(convert(*self.args, **self.kwargs))
示例19
def convert_file(src_file, target_folder='.'):
file_name = os.path.basename(src_file)
new_file_name = os.path.splitext(file_name)[0] + '.rst'
new_file_path = os.path.sep.join([target_folder, new_file_name])
print("**** convert: " + src_file + " to " + new_file_path)
if six.PY3:
open(new_file_path, "w").write(pypandoc.convert(src_file, 'rst'))
else:
open(new_file_path, "w").write(pypandoc.convert(src_file, 'rst').encode('utf8'))
示例20
def _make_note(self, note_data):
""" Converts a note from HTML to the configured markup.
If the note was previously edited with zotcli, the original markup
will be restored. If it was edited with the Zotero UI, it will be
converted from the HTML via pandoc.
:param note_html: HTML of the note
:param note_version: Library version the note was last edited
:returns: Dictionary with markup, format and version
"""
data = None
note_html = note_data['data']['note']
note_version = note_data['version']
if "title=\"b'" in note_html:
# Fix for badly formatted notes from an earlier version (see #26)
note_html = re.sub(r'title="b\"(.*?)\'"', r'title="\1"', note_html)
note_html = note_html.replace("\\n", "")
blobs = DATA_PAT.findall(note_html)
# Previously edited with zotcli
if blobs:
data = decode_blob(blobs[0])
if 'version' not in data:
data['version'] = note_version
note_html = DATA_PAT.sub("", note_html)
# Not previously edited with zotcli or updated from the Zotero UI
if not data or data['version'] < note_version:
if data and data['version'] < note_version:
self._logger.info("Note changed on server, reloading markup.")
note_format = data['format'] if data else self.note_format
data = {
'format': note_format,
'text': pypandoc.convert(
note_html, note_format, format='html'),
'version': note_version}
return data
示例21
def _make_note_html(self, note_data):
""" Converts the note's text to HTML and adds a dummy element that
holds the original markup.
:param note_data: dict with text, format and version of the note
:returns: Note as HTML
"""
extra_data = DATA_TMPL.format(
data=encode_blob(note_data).decode('utf8'))
html = pypandoc.convert(note_data['text'], 'html',
format=note_data['format'])
return html + extra_data
示例22
def convert_md():
with open(RST_README_PATH, 'w') as readme:
readme.write(convert(MD_README_PATH, 'rst'))
示例23
def read_md(file_path):
return convert(file_path, to='rst', format='asciidoc')
示例24
def convert_markdown_mathjax_for_rst(lines: List[str]) -> List[str]:
if all('$$' not in line for line in lines):
return lines
data = '\n'.join(lines)
sections = data.split('$$')
if len(sections) % 2 != 1:
raise ValueError('Mismatched number of "$$" latex tokens.')
result = []
for i, s in enumerate(sections):
if i % 2:
# Avoid getting split across divs.
s = ' '.join(s.split('\n'))
# Avoid intermediate layers turning our newlines into slashes.
s = s.replace('\\\\', r'\newline')
# Turn latex like "|x\rangle" into "|x \rangle".
# The extra space seems to be necessary to survive a later pass.
s = re.sub(r'([a-zA-Z0-9])\\', r'\1 \\', s)
# Keep the $$ so MathJax can find it.
result.append('$${}$$'.format(s))
else:
# Work around bad table detection in pandoc by concatenating
# lines from the same paragraph.
s = '\n\n'.join(e.replace('\n', ' ') for e in s.split('\n\n'))
# Convert markdown to rst.
out = pypandoc.convert(s, to='rst', format='markdown_github')
# Not sure why pandoc is escaping these...
out = out.replace(r'\|', '|')
result.extend(out.split('\n'))
return result
示例25
def autodoc_process(app, what: str, name: str, obj: Any, options,
lines: List[str]) -> None:
# Try to lookup in documented dictionary.
found = _doc.RECORDED_CONST_DOCS.get(id(obj))
if name.startswith('cirq') and found is not None:
# Override docstring if requested.
if found.doc_string is not None:
new_doc_string = inspect.cleandoc(found.doc_string)
lines[:] = new_doc_string.split('\n')
elif not (getattr(obj, '__module__', 'cirq') or '').startswith('cirq'):
# Don't convert objects from other modules.
return
# Don't convert output from Napoleon extension, which is already rst.
i = 0
while i < len(lines) and not lines[i].startswith(':'):
i += 1
if not i:
return
converted_lines = convert_markdown_mathjax_for_rst(lines[:i])
kept_lines = lines[i:]
data = pypandoc.convert(
'\n'.join(converted_lines),
to='rst',
format='markdown_github',
)
lines[:] = data.split('\n') + kept_lines
# -- Project information -----------------------------------------------------
示例26
def find_version(*file_paths):
version_file = read(*file_paths)
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
# Note: The package maintainer needs pypandoc and pygments to properly convert
# the Markdown-formatted README into RestructuredText before uploading to PyPi
# See https://bitbucket.org/pypa/pypi/issues/148/support-markdown-for-readmes
示例27
def read(filename):
filepath = os.path.join(os.path.dirname(__file__), filename)
try:
# Convert GitHub markdown to restructured text (needed for upload to PyPI)
from pypandoc import convert
return convert(filepath, 'rst')
except ImportError:
return open(filepath).read()
示例28
def read_md(f): return convert(f, 'rst', format='md')
示例29
def readme():
if make_sdist:
try:
import pypandoc
return pypandoc.convert('README.md', 'rst')
except ImportError:
print "Warning: the \"pypandoc\" package and/or the pandoc " \
"binary can't be found on your system: if you want to " \
"generate the README.rst for PyPI you'll need to install " \
"them properly, else a fallback description will be used."
# falling back to a simple description
return 'Simple version string management for git'
示例30
def readme():
with open('README.md', 'r') as f:
readme_md = f.read()
if pypandoc:
readme_rst = pypandoc.convert(readme_md, 'rst', format='md')
return readme_rst
else:
return readme_md