Exemplo n.º 1
0
def compose(args):
    """Composes the .md.in file at path."""

    path = args.path

    assert path.startswith('markdown/') and path.endswith('.md.in')
    
    meta = getmeta(path)
    meta['no-social'] = 'True'
    meta['rss-url'] = path[8:].replace('.md.in', '.xml')
    writemeta(meta)

    # Read the lines of the .md.in file
    lines = getcontent(path)

    # Process the lines
    writer = content_writer()
    next(writer)
    for line in lines:
        # If a filename is given then write the file (subject to some
        # processing); otherwise, write the line as-is.
        if line.strip().endswith('.md') and os.path.isfile(line.strip()):
            writer.send(line.strip())
        else:
            write(line)
Exemplo n.º 2
0
def preprocess(args):
    """Preprocesses path."""

    path = args.path

    # Load and write the metadata.  Obfuscate the title field as a workaround
    # to a pandoc bug.  This gets undone by postprocess.py.
    meta = getmeta(path)
    writemeta(meta, obfuscate=True)

    # Read in the lines
    lines = getcontent(path)

    # Insert the image into the lines
    if 'image' in meta:
        caption = meta['caption'] if 'caption' in meta else ''
        lines = insert_figure(lines, meta['image'], caption)

    # Replace the processing flags
    for i, line in enumerate(lines):

        # Clearing line break
        if line.strip() == '<!-- break -->':
            lines[i] = '<div style="clear: both; height: 0;"></div>\n'

        # Vertical space
        if line.strip() == '<!-- vspace -->':
            lines[i] = '<div style="clear: both; height: 3rem;"></div>\n'

    # Write out the new lines
    writelines(lines)
Exemplo n.º 3
0
def content_writer():
    """Writes the processed content of a .md file to stdout.

    Use it this way:

      writer = content_writer()
      next(writer)
      writer.send('/path/to/file.md')

    Send as many paths as you want.

    The content is processed internally and by pandoc (via a system call).
    The output is html.  For any subsequent processing with pandoc, don't
    forget to turn off its markdown_in_html_blocks extension.
    """

    # Process the entry template with pandoc-tpp and write it to a
    # temporary file.  Remove leading spaces so that this can be written into
    # a markdown file.
    lines = pandoc_tpp.preprocess('templates/entry.html5')
    with tempfile.NamedTemporaryFile(mode='w+', suffix='.html5',
                                     delete=False) as f:
        template_path = f.name
        f.writelines(lines)

    # Keep track of the number of files processed
    n = 0

    while True:

        # Get the path to the next entry
        path = yield
        assert path.startswith('markdown/')

        # Get various types of links
        relurl = path[8:-3] + '.html'
        plink = permalink(relurl)
        quoted_plink = urllib.parse.quote(plink).replace('/', '%2F')

        # Renew the entry's metadata
        meta = getmeta(path)
        meta['rel-url'] = relurl
        meta['permalink'] = plink

        # Flag the first entry in the metadata
        if n == 0:
            meta['first-entry'] = 'True'
        elif 'first-entry' in meta:
            del meta['first-entry']

        # Process the entry's content
        lines = process(getcontent(path), meta, n)

        # Write the markdown to a temporary file
        with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
            tmppath1 = f.name
            writemeta(meta, f=f)
            writelines(lines, f=f)

        # Preprocess the temporary file if this is the first entry
        if n==0:
            with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
                tmppath2 = f.name
                subprocess.call(['bcms', 'preprocess', tmppath1], stdout=f)
        else:
            tmppath2 = tmppath1
        
        # Write a horizontal rule between files
        if n != 0:
            write('\n<hr />\n')

        # Start the new entry
        write('\n')
        write('<div id="entry-%d">\n'%n)

        # Process the markdown with pandoc, writing the output to stdout
        STDOUT.flush()
        assert path.startswith('markdown/') and path.endswith('.md')
        subprocess.call(['pandoc',  tmppath2,
                         '-s', '-S',
                         '-f', 'markdown+markdown_attribute',
                         '-t', 'html5',
                         '--email-obfuscation', 'none',
                         '--template', template_path,
                         '-M', 'permalink=' + plink,
                         '-M', 'quoted-permalink=' + quoted_plink])
        STDOUT.flush()

        write('</div> <!-- id="entry-%d" -->\n'%n)

        # Increment the counter
        n += 1