Example #1
0
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 \
             os.path.isfile(line.strip())][:10]

    # Create the RSS
    rss = rss2.RSS2(generator=None,
                    docs=None,
                    title=title,
                    link=permalink(path[8:].replace('.md.in', '.html')),
                    description=subtitle,
                    lastBuildDate=datetime.datetime.now(),
                    items=items)

    # Write it out
    write(rss.to_xml())
Example #2
0
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,
                        guid=guid)
Example #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