Example #1
def make_item(path):
    """Makes an item from .md file at path and its associated .html file."""

    assert path.startswith('markdown/') and path.endswith('.md')

    # Read and process the .md file
    meta = getmeta(path)

    # Get the permalink and guid
    plink = permalink(path[8:].replace('.md', '.html'))
    guid = rss2.Guid(plink)

    # Get the metadata
    pubdate = meta['date'] if 'date' in meta else None
    author = meta['author'] if 'author' in meta else None
    publisher = meta['publisher'] if 'publisher' in meta else None
    source = meta['source'] if 'source' in meta else None

    # Construct the item source
    source = rss2.Source(publisher, source) if publisher and source else None

    # Get the html body
    path = path.replace('.md', '.html')
    path = path.replace('markdown', 'www')
    description = encode(get_content_body(path))

    # Style the figure caption
    description = description.replace('<figcaption>',
                                      '<figcaption style="font-size: 80%;">')

    return rss2.RSSItem(title=meta['title'], link=plink, pubDate=pubdate,
                        author=author, source=source, description=description,
Example #2
def feed(args):
    """Makes a feed from an .md.in file."""

    path = args.path

    # Extract the metadata fields we need
    meta = getmeta(path)
    title = meta['rsstitle'] if 'rsstitle' in meta else None
    subtitle = meta['subtitle'] if 'subtitle' in meta else ''

    # Get the first ten RSS items from file listing at path
    lines = getcontent(path)
    items = [make_item(line.strip()) for line in lines \
             if line.strip().endswith('.md') and \

    # Create the RSS
    rss = rss2.RSS2(generator=None,
                    link=permalink(path[8:].replace('.md.in', '.html')),

    # Write it out
Example #3
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')

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

    # Process the lines
    writer = content_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()):
Example #4
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
Example #5
def content_writer():
    """Writes the processed content of a .md file to stdout.

    Use it this way:

      writer = content_writer()

    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

    # 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)
            tmppath2 = tmppath1
        # Write a horizontal rule between files
        if n != 0:
            write('\n<hr />\n')

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

        # Process the markdown with pandoc, writing the output to stdout
        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])

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

        # Increment the counter
        n += 1