def migrate(phab_url, api_token):
    for fname in glob.glob(sys.argv[1] + '/*.md'):
        bname = os.path.basename(fname)[:-3]
        title = string.capwords(bname.replace('-', ' '))
        slug = title.lower()
        slug = re.sub('[^a-z]', '_', slug)
        slug = re.sub('_{2,}', '_', slug)

        print fname
        print title
        print slug

        remarkup_f = StringIO()
        with open(fname) as f:
            pandoc('--from', 'markdown_github-yaml_metadata_block',
                   to='remarkup.lua',
                   _in=f,
                   _out=remarkup_f)
        remarkup_f.seek(0)

        data = {'api.token': api_token,
                'slug': slug,
                'title': title,
                'content': remarkup_f.read()}
        resp = requests.get(
            urlparse.urljoin(phab_url, '/api/phriction.create'),
            data=data)
        resp.raise_for_status()
        print 'Response:', resp.json()
        print
Example #2
0
def main():
    "do the work"
    outdir = os.getcwd() # TODO parameterize
    outfile = os.path.join(outdir, "output.json")
    outlist = []
    with cd("_site"):  # TODO unhardcode
        for root, dirs, files in os.walk(".", topdown=False):
            for name in files:
                if name.endswith(".html"):
                    filename = os.path.join(root, name)
                    url = filename.lstrip(".")
                    print(url)
                    doc = lxml.html.parse(filename)
                    title = doc.find(".//title").text.replace(" - Fred Hutch Biomedical Data Science Wiki", "")
                    # print(title)
                    outio = StringIO()
                    sh.pandoc("-f", "html", "-t", "plain", filename, _out=outio)
                    # print(outio.getvalue())
                    outdict=dict(type="add",
                                 id=url,
                                 fields=dict(title=title, content=outio.getvalue()))
                    outlist.append(outdict)
                    # with open(outfile, "w") as outfh:
                    #     json.dump(outdict, outfh)


        # for name in dirs:
        # print(os.path.join(root, name))

    with open(outfile, "w") as outfh:
        json.dump(outlist, outfh)
Example #3
0
def run(p, outfile_name, domains):
    outfile_md_name = outfile_name + ".md"

    md_fd = open(outfile_md_name, "w")
    for domain in domains:
        output_summary(p, md_fd, domain)

    md_fd.close()

    pandoc("-s", "-o", outfile_name, outfile_md_name)
Example #4
0
def sh_pandoc(input_files: List[str], output_filename: str, cwd_path: str):
    pandoc(" ".join(input_files),
           '--output',
           output_filename,
           '--from', 'markdown+tex_math_single_backslash+raw_tex+'
           'table_captions+yaml_metadata_block+autolink_bare_uris',
           '--pdf-engine=xelatex',
           '--citeproc',
           '--resource-path=' + cwd_path,
           '--standalone',
           _cwd=cwd_path)
def make_pdf(filename):
    parent = str(list(filename.parents)[0])
    file_parent = ''
    if parent.endswith('algebra'): 
        file_parent = 'LA-'
    elif parent.endswith('datastructures'):
        file_parent = 'GAD-'

    sh.pandoc(f'{filename}', '-f', 'markdown', '-t', 'latex',
              '-o', f'/Users/az/Uni/pdf/{file_parent}{filename.stem}.pdf', 
              '-S', '--latex-engine=xelatex')
Example #6
0
def preview_note(no: int):
    pandoc = shutil.which('pandoc')
    if pandoc is None:
        logger.error('Pandoc not installed?\nInstall it with '
                     '`apt install pandoc before running this command.')
        return
    with open(REC_FILE) as f:
        paths = [line.strip() for line in f.readlines()]
    fn = paths[no - 1]
    preview_file = Path(fn).parent / 'preview.html'
    sh.pandoc(fn, standalone=True, mathjax=True, toc=True, output=preview_file)
    webbrowser.open(str(preview_file))
Example #7
0
def sh_pandoc(input_files, output_filename, cwd_path):
    pandoc(
        " ".join(input_files),
        '--output',
        output_filename,
        '--from',
        'markdown+ascii_identifiers+tex_math_single_backslash+raw_tex+table_captions+yaml_metadata_block+autolink_bare_uris',
        '--pdf-engine=xelatex',
        '--filter',
        'pandoc-citeproc',
        '--resource-path=' + cwd_path,
        '--standalone',
        _cwd=cwd_path)
Example #8
0
def create_web_page_list():
    page_home = Path(configs['app_home']) / LOCAL_REPO
    vcs = Path(configs['app_home']) / LOCAL_REPO / '.git'
    converted = {'note': []}
    if vcs.exists():
        # incremental sync, only notes updated since last blog commit will
        # be converted to blog pages
        # TODO
        pass
    else:
        logger.info('No VCS found in your blog folder.\n'
                    'A complete conversion will be performed')
        page_home.mkdir(exist_ok=True)
        nb = configs['blog.notebook']
        for note in NOTE_FILES:
            with open(note, 'r', encoding='utf-8') as f:
                lines = f.readlines()
            if nb not in lines[NOTEBOOK_LINE_NO]:
                continue
            target = note.stem + '.html'
            note_yml = Path('/tmp') / note.name
            sh.sed(sh.sed(note, e="1 i ---"), e='7 i ---', _out=str(note_yml))
            sh.pandoc(note_yml,
                      standalone=True,
                      mathjax=True,
                      toc=True,
                      template=NOTE_TEMP,
                      output=str(page_home / target))
            converted['note'].append({
                'title':
                lines[TITLE_LINE_NO].strip()[7:],
                'tags':
                lines[TAG_LINE_NO].strip()[6:]
                if len(lines[TAG_LINE_NO].strip()) > 7 else '',
                'path':
                './' + target,
                'updated':
                lines[UPD_LINE_NO].strip()[9:]
            })

    with open(NOTE_LIST_PATH, 'w', encoding='utf-8') as f:
        notesyml = yaml.dump(converted,
                             default_flow_style=False,
                             allow_unicode=True)
        f.write(f'---\n{notesyml}---')

    sh.pandoc(NOTE_LIST_PATH,
              standalone=True,
              mathjax=True,
              template=HOMEPAGE_TEMP,
              output=str(page_home / HOMEPAGE))
Example #9
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]
    options, args = script.parse("help input= output= debug", args)
    if options.help:
        print help()
        sys.exit(0)
    elif not args or len(args) > 1:
        print help()
        sys.exit(1)
    else:
        module_name = args[0]

    module = importlib.import_module(module_name)
    filename = script.first(options.input) or inspect.getsourcefile(module)
    if filename is None:
        raise RuntimeError("missing input filename")
    source = open(filename).read()

    debug = bool(options.debug)

    markdown = docgen(module, source, debug)
    if not options.output:
        print markdown
    else:
        output = script.first(options.output)
        basename = os.path.basename(output)
        if len(basename.split(".")) >= 2:
            ext = basename.split(".")[-1]
        else:
            ext = None
        if ext == "tex":
            sh.pandoc(read="markdown", toc=True, standalone=True, write="latex", o=output, _in=markdown)
        elif ext == "pdf":
            try: # keep that somewhere, but use pandoc to generate the pdf ?
                latex = ".".join(basename.split(".")[:-1]) + ".tex"
                build = tempfile.mkdtemp()
                cwd = os.getcwd()
                os.chdir(build)
                sh.pandoc(read="markdown", toc=True, standalone=True, write="latex", o=latex, _in=markdown)
                sh.xelatex(latex)
                sh.xelatex(latex)
                os.chdir(cwd)
                sh.cp(os.path.join(build, latex[:-4] + ".pdf"), output)
            finally:
                try:
                    shutil.rmtree(build) # delete directory
                except OSError, e:
                    if e.errno != 2: # code 2 - no such file or directory
                        raise
        else:
def migrate(phab_url, api_token, wiki_path, slug_prefix):
    files = glob.glob(os.path.join(wiki_path, '*.md'))
    files.extend(glob.glob(os.path.join(wiki_path, '*.markdown')))
    failed_files = []
    for fname in files:
        bname = os.path.splitext(os.path.basename(fname))[0]
        title = string.capwords(bname.replace('-', ' '))
        slug = title.lower()
        slug = re.sub('[^a-z]', '_', slug)
        slug = re.sub('_{2,}', '_', slug)
        if slug_prefix:
            slug = slug_prefix + slug

        print fname
        print title
        print slug

        remarkup_f = StringIO()
        with open(fname) as f:
            pandoc('--from',
                   'markdown_github-yaml_metadata_block',
                   to='remarkup.lua',
                   _in=f,
                   _out=remarkup_f)
        remarkup_f.seek(0)

        data = {
            'api.token': api_token,
            'slug': slug,
            'title': title,
            'content': remarkup_f.read()
        }
        resp = requests.get(urlparse.urljoin(phab_url,
                                             '/api/phriction.create'),
                            data=data)
        try:
            resp.raise_for_status()
            print 'Response:', resp.json()
        except Exception as e:
            failed_files.append({'name': fname, 'err': str(e)})

        print

    if len(failed_files) > 0:
        print '**************************************************************'
        print 'Failed files: '
        for failed_file in failed_files:
            print failed_file['name']
            print failed_file['err']
 def convert(match):
     source = match.groups()[0]
     source = '\n'.join(l.strip() for l in source.split('\n'))
     source = "<pre>%s</pre>" % source
     rst_source = pandoc(echo(source), f='html', t='rst').stdout.decode('utf8')
     # rst_source = rst_source.strip().replace('\n', '\n   ') + '\n'
     return rst_source
Example #12
0
def markdownify(pandocJson):
    if type(pandocJson) != type([]):
        pandocJson = [pandocJson]

    doc = [{"unMeta": {}}, pandocJson]
    out = pandoc("-f", "json", "-t", "markdown", _in=json.dumps(doc))
    return str(out)
Example #13
0
 def _generate_release_diff(self):
     diff = sh.rpc_differ(self.tag.previous, self.tag, update=True).stdout
     return sh.pandoc(
         '--from', 'rst',
         '--to', 'markdown_github',
         _in=diff
     ).stdout
Example #14
0
def from_markdown(string):
    """
    Read a markdown text as a Pandoc instance.
    """
    json_str = str(sh.pandoc(read="markdown", write="json", _in=string))
    json_ = json.loads(json_str, object_pairs_hook=Map)
    return from_json(json_)
Example #15
0
def from_markdown(string):
    """
    Read a markdown text as a Pandoc instance.
    """
    json_str = str(sh.pandoc(read="markdown", write="json", _in=string))
    json_ = json.loads(json_str, object_pairs_hook=Map)
    return from_json(json_)
Example #16
0
 def _info_description(self):
     # get description node
     _ = self.document.css(".app-desc ~ div")
     # get inner html
     _ = "".join(_.xpath("node()").extract())
     # to github flavoured markdown
     return pandoc(f="html", to="gfm", columns=80, _in=_).rstrip()
 def convert(match):
     source = match.groups()[0]
     source = '\n'.join(l.strip() for l in source.split('\n'))
     source = "<pre>%s</pre>" % source
     rst_source = pandoc(echo(source), f='html', t='rst').stdout.decode('utf8')
     # rst_source = rst_source.strip().replace('\n', '\n   ') + '\n'
     return rst_source
def migrate(phab_url, api_token, wiki_path, slug_prefix):
    files = glob.glob(os.path.join(wiki_path, '*.md'))
    files.extend(glob.glob(os.path.join(wiki_path, '*.markdown')))
    failed_files = []
    for fname in files:
        bname = os.path.splitext(os.path.basename(fname))[0]
        title = string.capwords(bname.replace('-', ' '))
        slug = title.lower()
        slug = re.sub('[^a-z]', '_', slug)
        slug = re.sub('_{2,}', '_', slug)
        if slug_prefix:
            slug = slug_prefix + slug

        print fname
        print title
        print slug

        remarkup_f = StringIO()
        with open(fname) as f:
            pandoc('--from', 'markdown_github-yaml_metadata_block',
                   to='remarkup.lua',
                   _in=f,
                   _out=remarkup_f)
        remarkup_f.seek(0)

        data = {'api.token': api_token,
                'slug': slug,
                'title': title,
                'content': remarkup_f.read()}
        resp = requests.get(
            urlparse.urljoin(phab_url, '/api/phriction.create'),
            data=data)
        try:
            resp.raise_for_status()
            print 'Response:', resp.json()
        except Exception as e:
            failed_files.append({'name': fname, 'err': str(e)})

        print

    if len(failed_files) > 0:
        print '**************************************************************'
        print 'Failed files: '
        for failed_file in failed_files:
            print failed_file['name']
            print failed_file['err']
Example #19
0
 def _generate_release_diff(self):
     logging.info("Generating release diff...")
     diff = sh.rpc_differ(self.tag.previous,
                          self.tag,
                          "--rpc-repo-url",
                          self.repo.url,
                          update=True).stdout
     return sh.pandoc('--from', 'rst', '--to', 'markdown_github',
                      _in=diff).stdout
Example #20
0
def read(text):
    """
    Read a markdown text as a Pandoc instance.
    """
    #print "***text:", text
    json_text = str(sh.pandoc(read="markdown", write="json", _in=text))
    json_ = json.loads(json_text)
    #import pprint
    #pp = pprint.PrettyPrinter(indent=2).pprint
    #print "***json:"
    #pp(json_)
    return to_pandoc(json_)
Example #21
0
 def _generate_release_diff(self):
     logging.info("Generating release diff...")
     diff = sh.rpc_differ(self.tag.previous,
                          self.tag,
                          "--rpc-repo-url", self.repo.url,
                          update=True
                          ).stdout
     return sh.pandoc(
         '--from', 'rst',
         '--to', 'markdown_github',
         _in=diff
     ).stdout
Example #22
0
def convert_entries(entries):
    """Convert entries"""
    
    if exists(NEW_ROOT):
        rmtree(NEW_ROOT)
    mkdir(NEW_ROOT)
    for filename, entry in entries.items():
        #info(filename, entry)
        if not exists(NEW_ROOT+entry['path']):
            makedirs(NEW_ROOT+entry['path'])
        fn_base, fn_ext = splitext(basename(filename))
        fn = CONTENT_ROOT+entry['path']+filename
        fn_mdn = NEW_ROOT+entry['path']+fn_base+'.md'
        if fn_ext == '.html':
            #stat_ori = stat(fn)
            #iconv('-t', 'utf-8', '-o', fn, fn)
            #utime(fn, (stat_ori[ST_ATIME], stat_ori[ST_MTIME]))
            pandoc('--atx-headers', '--from', 'html', '--to', 'markdown',
                '--strict', '-s', '--no-wrap', '-o', fn_mdn, fn)
            transform_markdown(fn_mdn, entry)
        else:
            copy(fn, NEW_ROOT+entry['path'])
            print("Copied unknown filetype %s" %filename)
Example #23
0
def main():
    src = path.path("src")

    dst = path.path("dst")
    if not dst.exists():
        dst.mkdir()
    for file in dst.files():
        file.remove()

    md_files = [file for file in src.listdir() if file.ext in [".txt", ".md"]]
    md_files.sort(key=lambda file: file.getsize())
    for md_file in md_files:
        namebase = md_file.namebase
        print "{0:20} ...".format(namebase[:20]), 
        js_file = namebase + ".js"

        error = False
        try:
            sh.pandoc("-t", "json", "-o", dst / js_file, md_file)
            json1 = json.load(open(dst / js_file), object_pairs_hook=pandoc.Map)
            doc = pandoc.to_pandoc(json1)
            py_file = namebase + ".py"
            output = open(dst / py_file, "w")
            output.write(repr(doc))
            js2_file = namebase + "2" + ".js"
            json2 = pandoc.to_json(doc)
            json.dump(json2, open(dst /js2_file, "w"))
            sh.pandoc("-s",
                      "-t", "markdown", "-o", dst / namebase + ".txt", 
                      "-f", "json", dst / js2_file)
        except Exception:
            error = True        

        if not error and json1 == json2:
            print "OK"
        else:
            print "FAIL"
Example #24
0
def markdown_to_html_pandocs(text):
    """
    This works well on some South American airports
    """
    output_file = tempfile.NamedTemporaryFile(delete=False)
    file_name = output_file.name
    output_file.close()

    with codecs.open(file_name, 'w+b', encoding='utf-8') as write_file:
        write_file.write(text)

    html = str(
        pandoc('-f', 'markdown_phpextra', '-t', 'html', output_file.name))
    os.unlink(file_name)

    return html
Example #25
0
def latex():
    with codecs.open("temp.md", "w+", "utf-8") as f:
        f.write(request.form["content"])
    args = "temp.md --latex-engine=xelatex -t beamer -V theme:Carsurfing -o temp.pdf --smart"
    args = "temp.md --latex-engine=xelatex -t beamer -o temp.pdf --smart"
    pandoc_status = pandoc(*args.split(" "))

    rm_status = rm(sh.glob("static/images/*"), "-r")

    args = "-quality 100 -density 200x200 temp.pdf static/images/output%d.jpg"
    convert_status = convert(*args.split(" "))

    image_dir = "static/images"
    files = os.listdir(image_dir)

    print pandoc_status, rm_status, convert_status

    return json.dumps(files)
Example #26
0
def markdown_to_html_pandocs(text):
    """
    This works well on some South American airports
    """
    output_file = tempfile.NamedTemporaryFile(delete=False)
    file_name = output_file.name
    output_file.close()

    with codecs.open(file_name, 'w+b', encoding='utf-8') as write_file:
        write_file.write(text)

    html = str(pandoc('-f',
                      'markdown_phpextra',
                      '-t',
                      'html',
                      output_file.name))
    os.unlink(file_name)

    return html
Example #27
0
def index():
    if request.method == 'POST':
        uuid = str(uuid4())
        f = request.files['file']
        f.save('/tmp/%s.md' % (uuid, ))
        pdf_filename = '/tmp/%s.pdf' % (uuid, )
        out = pandoc('/tmp/%s.md' % (uuid, ), "-o", pdf_filename, "--template",
                     "brief.tex")
        remove('/tmp/%s.md' % (uuid, ))
        return send_file(pdf_filename,
                         attachment_filename=path.splitext(f.filename)[0] +
                         ".pdf",
                         add_etags=False,
                         as_attachment=True)
    else:
        html = """<html><body><form action="/" method="post" enctype="multipart/form-data"> <input name="file" type="file" size="50" accept="text/*"> <button type="submit">Upload</button></form>
<p>Keep in mind, that this is just a proof of concept. Download the <a href="https://gist.github.com/brejoc/6774bd9974b71a48b624520de788d0de" target="_blank">example Markdown file</a> from GitHub and upload it here. You should get a nicely formatted German DIN-letter.</p>
</body></html>

"""
        return html
Example #28
0
def html_to_text(html):
    "convert html to text"
    infile = tempfile.mkstemp()
    with open(infile[1], "w") as in_fh:
        in_fh.write(html)
    outfile = tempfile.mkstemp()

    pandoc_result = sh.pandoc(
        "-f",
        "html",
        "-t",
        "plain",
        "-o",
        outfile[1],
        infile[1],
        _tty_out=False,
        _tty_in=False,
    )
    print("exit code is {}".format(pandoc_result.exit_code))
    with open(outfile[1]) as outfh:
        output = outfh.read()
    os.remove(infile[1])
    os.remove(outfile[1])
    return output
Example #29
0
def to_markdown(doc):
    """
    Write a Pandoc instance as a markdown text.
    """
    json_str = json.dumps(to_json(doc))
    return str(sh.pandoc("-s", read="json", write="markdown", _in=json_str))
Example #30
0
"""
    raise ImportError(error.format(req=" ".join(setup_requires)))
import about
import sh

# Python Runtime Dependencies
requirements = dict(install_requires="sh")

# Local
sys.path.insert(0, "")
import about_pandoc

# Non-Python Runtime Dependencies 
try:
    pandoc = sh.pandoc
    magic, version = sh.pandoc("--version").splitlines()[0].split()
    assert magic == "pandoc"
    assert version.startswith("1.12") or version.startswith("1.13")
except:
    # if root runs the setup script, it's ok if pandoc is not available,
    # as long as the users have it. Hence we cannot raise an exception here,
    # we only produce a warning. 
    warnings.warn("cannot find pandoc 1.12 / 1.13")

# ------------------------------------------------------------------------------

contents = dict(py_modules=["pandoc", "about_pandoc"])
metadata = about.get_metadata(about_pandoc)
        
info = {}
info.update(contents)
Example #31
0
 def compile_pdf(self, in_file, out_file):
     # pandoc -r markdown slides.md -t beamer -o out.pdf --slide-level=3 --toc --highlight-style=tango
     pandoc('-r', 'markdown', in_file, '-t', 'beamer', '-o', out_file, _cwd=self.cwd)
Example #32
0
def mmd2docx(markdown_file_name, docx_file_name):
    sh.pandoc(markdown_file_name, f="markdown_mmd", t="docx", o=docx_file_name)
Example #33
0
from setuptools import setup

try:
    from sh import pandoc

    isPandoc = True
except ImportError:
    isPandoc = False

# Get the long description from the README file
readmepath = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'README.md')
long_description = ''
if os.path.exists(readmepath):
    if isPandoc:
        long_description = pandoc(readmepath, read='markdown', write='rst')
    else:
        long_description = open(readmepath, encoding='utf-8').read()

setup(
    name='mdutils',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='1.3.0',

    description='Python package for working with Markdown. Includes `docxtomd`, a Word .docx to Markdown converted '
                'using `pandoc` and `wmf2svg`, and `wmftosvgpng`, an intelligent WMF to SVG or PNG converter using '
                '`wmf2svg`.',
    long_description=long_description,
Example #34
0
def to_markdown(doc):
    """
    Write a Pandoc instance as a markdown text.
    """
    json_str = json.dumps(to_json(doc))
    return str(sh.pandoc("-s", read="json", write="markdown", _in=json_str))
Example #35
0
def generate_rst():    
  sh.pandoc('-f', 'markdown', '-t', 'rst', '-o', 'README.rst', 'README.md', _out=sys.stdout, _err_to_out=True)
  sh.pandoc('-f', 'markdown', '-t', 'rst', '-o', 'CHANGES.rst', 'CHANGES.md', _out=sys.stdout, _err_to_out=True)
  filenames_diff = sh.git('diff', '--name-only')
  if 'README.rst' in filenames_diff or 'CHANGES.rst' in filenames_diff:
    sh.git('commit', 'README.rst', 'CHANGES.rst', '-m', 'Autogenerated from markdown files', _out=sys.stdout, _err_to_out=True)
Example #36
0
def md_to_pdf_with_template(md_file, pdf_file, template):
    """ Converts from Markdown to PDF using a template. """
    pandoc(md_file, '-o', pdf_file, '--template={}'.format(template))
Example #37
0
def latex_to_pdf(latex_file='.tmp.tex', pdf_file='output.pdf'):
    """ Converts from LaTeX to PDF. """
    pandoc(latex_file, '-o', pdf_file)
Example #38
0
            book.append(text)

from jinja2 import Environment
env = Environment()
import macros
env.globals.update(vars(macros))
t = env.from_string('\n\n'.join(book))
book = t.render()

with open(".temp.md", 'w') as f:
    f.write(book)


pandoc = pandoc.bake('.temp.md', f='markdown',
                                 smart=True,
                                 toc=True,
                                 standalone=True,
                                 chapters=True)
pandoc(output="build/book.pdf", template="src/template")
pandoc(output="build/book.html", t="html5")
pandoc(output="build/book.odt")
pandoc(output="build/book.md", t="markdown_github")
os.unlink('.temp.md')

bads = check('\n\n'.join(book))
if bads:
    print "The following should be addressed:"
    for bad_type, bad in bads:
        print bad_type, bad
        print bad_type.title(), ":", bad
Example #39
0
def write(doc):
    """
    Write a Pandoc instance as a markdown text.
    """
    json_text = json.dumps(to_json(doc))
    return str(sh.pandoc(read="json", write="markdown", _in=json_text))
def other_formats(name):
    if not args.formats:
        return
    for fmt in args.formats.split(","):
        sh.pandoc(name, "--from", "markdown", "-s", "-o",
                  "%s.%s" % (name, fmt))
Example #41
0
import argparse
import copy as _copy
import collections
import contextlib
import json
import os.path
import re
import sys

# Third-Party Libraries
import sh

# Non-Python Dependencies
try:
    pandoc = sh.pandoc
    magic, version = sh.pandoc("--version").splitlines()[0].split()
    assert magic == "pandoc"
    assert version.startswith("1.12") or version.startswith("1.13")
except:
    raise ImportError("cannot find pandoc 1.12 / 1.13")

# TODO: rethink the tuple thing. Tuple may yield a structure closer to the
#       original one, but also limit the mutability. Replace tuples with
#       list and update the typechecking accordingly ? (even in maps ?)

# TODO: analyze jQuery-like style to implement (mutable) transformations.
#       Selectors mutable transforms that apply on all elements of the list.
#       What would selector look like ? getattr magic to be applied to a
#       list-derived type ?

#
Example #42
0
 def _generate_release_diff(self):
     diff = sh.rpc_differ(self.tag.previous, self.tag, update=True).stdout
     return sh.pandoc('--from', 'rst', '--to', 'markdown_github',
                      _in=diff).stdout
Example #43
0
pydocs = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'docs',
                      'pydocs')
if not os.path.isdir(pydocs):
    os.makedirs(pydocs)
pdoc(
    'feaLab',
    _cwd=pydocs,
    html=True,
    all_submodules=True,
    external_links=True,
    overwrite=True,
)

try:
    from sh import pandoc

    isPandoc = True
except ImportError:
    warnings.warn("Run: brew install pandoc")
    isPandoc = False

if isPandoc:
    readmepath = os.path.join(os.path.realpath(os.path.dirname(__file__)),
                              'README.md')
    if os.path.exists(readmepath):
        long_description = pandoc(readmepath,
                                  read='markdown',
                                  write='html',
                                  output='docs/index.html')
Example #44
0
from setuptools import setup

try:
    from sh import pandoc

    isPandoc = True
except ImportError:
    isPandoc = False

# Get the long description from the README file
readmepath = os.path.join(os.path.realpath(os.path.dirname(__file__)),
                          "README.md")
long_description = ""
if os.path.exists(readmepath):
    if isPandoc:
        long_description = pandoc(readmepath, read="markdown", write="rst")
    else:
        long_description = open(readmepath, encoding="utf-8").read()

setup(
    name="mdx_steroids",
    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version="0.6.1",
    description="Small collection of Python Markdown extensions",
    long_description=long_description,
    # The project's main homepage.
    url="https://github.com/twardoch/markdown-steroids/",
    download_url=
    "https://github.com/twardoch/markdown-steroids/archive/master.zip",
Example #45
0
def mmd2rtf(markdown_file_name,rtf_file_name):
    sh.pandoc(markdown_file_name,f="markdown_mmd",t="rtf",o=rtf_file_name)
Example #46
0
def migrate_to_git():
    if arguments['--users-file']:
        users = json.loads(open(arguments['<users_file>']).read())
    else:
        users = parse_users()
    git_repo = arguments['<git_repo>']

    if not os.path.exists(git_repo):
        os.makedirs(git_repo)
    if not os.path.exists(os.path.join(git_repo, '.git')):
        git.init(git_repo)

    data_dir = os.path.abspath(arguments['<data_dir>'])
    root = os.path.join(data_dir, 'pages')
    pages = os.listdir(root)
    os.chdir(git_repo)
    for page in pages:
        if page in CONVERSION_BLACKLIST:
            continue
        versions = get_versions(page, users=users, data_dir=data_dir)
        if not versions:
            print("### ignoring %s (no revisions found)" % page)
            continue
        path = _hyphenize(_unquote(page)) + '.txt'
        print("### Creating %s\n" % path)
        dirname, basename = os.path.split(path)
        if dirname and not os.path.exists(dirname):
            os.makedirs(dirname)

        for version in versions:
            revision = version.pop('revision')
            # Handle attachment revisions
            if revision == '99999999':
                continue
            print("revision %s" % revision)
            try:
                if version['content']:
                    with open(path, 'w') as f:
                        print("Opening %s" % path)
                        f.write(version.pop('content'))
                    print("Adding %s" % path)
                    git.add(path)
                else:
                    print("Removing %s" % path)
                    git.rm(path)
                    version.pop('content')
                if version['rst_content']:
                    with open(path.replace('txt', 'rst'), 'w') as f:
                        print("Opening %s" % path.replace('txt', 'rst'))
                        f.write(version.pop('rst_content'))
                    pandoc(path.replace('txt', 'rst'),
                           f="rst",
                           t="markdown_github",
                           o=path.replace('txt', 'md'))
                    print("Adding %s" % path.replace('txt', 'rst'))
                    git.add(path.replace('txt', 'rst'))
                    print("Adding %s" % path.replace('txt', 'md'))
                    git.add(path.replace('txt', 'md'))
                elif os.path.isfile(path.replace('txt', 'rst')):
                    print("Removing %s" % path.replace('txt', 'rst'))
                    git.rm(path.replace('txt', 'rst'))
                    print("Removing %s" % path.replace('txt', 'md'))
                    git.rm(path.replace('txt', 'md'))
                    version.pop('rst_content')
                else:
                    version.pop('rst_content')
                print("Committing %s" % path)
                print(version['m'])
                if not version['m'].strip():
                    version['m'] = "Change made on %s" % version[
                        'date'].strftime('%x')
                git.commit(path.replace('txt', '*'), **version)
            except Exception as e:
                print(e)
Example #47
0
def mmd2docx(markdown_file_name,docx_file_name):
    sh.pandoc(markdown_file_name,f="markdown_mmd",t="docx",o=docx_file_name)
Example #48
0
def mmd2rtf(markdown_file_name, rtf_file_name):
    sh.pandoc(markdown_file_name, f="markdown_mmd", t="rtf", o=rtf_file_name)
Example #49
0
def get_metadata(module):
    """
    Get the metadata content from the module argument.

    This function uses the following variables when they are defined:

        __name__
        __appname__
        __version__
        __license__
        __author__
        __url__
        __doc__
        __docformat__
        __classifiers__

    It returns a `metadata` dictionary that provides keywords arguments
    for the setuptools `setup` function.
    """

    about_data = module.__dict__
    metadata = {}

    # Read the relevant __*__ module attributes.
    names = """
        __name__
        __appname__
        __version__
        __license__
        __author__
        __url__
        __doc__
        __docformat__
        __classifiers__
    """
    for name in names.split():
        value = about_data.get(name)
        if value is not None:
            metadata[name[2:-2]] = value

    # Search for author email with a <...@...> syntax in the author field.
    author = metadata.get("author")
    if author is not None:
        email_pattern = r"<([^>]+@[^>]+)>"
        match = re.search(email_pattern, author)
        if match is not None:
            metadata["author_email"] = email = match.groups()[0]
            metadata["author"] = author.replace("<" + email + ">", "").strip()
        else:
            metadata["author"] = author

    # Get the module summary and description from the docstring.

    # Process the doc format first (markdown is the default format)
    doc = metadata.get("doc")
    if doc is not None:
        docformat = metadata.get("docformat", "markdown").lower()
        if "rest" in docformat or "restructuredtext" in docformat:
            pass
        elif "markdown" in docformat:
            # Try to refresh the ReST documentation in 'doc/doc.rst'
            try:
                pandoc = sh.pandoc
                try:
                    sh.mkdir("doc")
                except sh.ErrorReturnCode:
                    pass
                sh.pandoc("-o", "doc/doc.rst", _in=doc)            
            except sh.CommandNotFound, sh.ErrorReturnCode:
                warning = "warning: cannot generate the ReST documentation."
                print >> sys.stderr, warning
           # Fallback on the old 'doc/doc.rst' file if it exists.
            try:
                doc = path("doc/doc.rst").open().read()
            except IOError, sh.ErrorReturnCode:
                doc = None # there is nothing we can do at this stage.
                warning = "warning: unable to use existing ReST documentation."
                print >> sys.stderr, warning
Example #50
0
from distutils.core import setup
from sh import pandoc

setup(
    name='cardscript',
    version='0.6',
    description="A scriptable card game processing engine.",
    author="Charles Nelson",
    author_email="*****@*****.**",
    url="https://github.com/cnelsonsic/cardscript",
    packages=['cardscript', 'cardscript.cards'],
    license='AGPLv3+',
    long_description='\n'.join(pandoc('README.md', t='rst')),
    classifiers=[
        'Development Status :: 3 - Alpha',
        'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
        'Intended Audience :: End Users/Desktop',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: Implementation :: PyPy',
        'Topic :: Games/Entertainment :: Board Games',
        ],
)

Example #51
0
 def compile_html(self, in_file, out_file):
     template = self.file_dir
     pandoc('--section-divs', '-t', 'html5', '-s', '--template', self.template, '-o', out_file, in_file, _cwd=self.cwd)
Example #52
0
def md_to_pdf(md_file='.tmp.md', pdf_file='output.pdf'):
    """ Converts from Markdown to PDF. """
    pandoc(md_file, '-o', pdf_file)
Example #53
0
def md_to_rst(markdown_text):
    """
    Convert a string containing markdown to restructured text.
    """
    cmd = pandoc(echo(markdown_text), "--from", "markdown", "--to", "rst")
    return cmd.stdout
Example #54
0
def md_to_latex(md_file='.tmp.md', latex_file='.tmp.tex'):
    """ Converts from Markdown to LaTeX. """
    pandoc(md_file, '-o', latex_file)
Example #55
0
def compile(input_file_path, output_file_path):
    print("compiling: %s to %s " % (input_file_path, output_file_path))
    pandoc(input_file_path,
           c="pandoc.css",
           f="markdown+lhs",
           o=output_file_path)