示例#1
0
def parse_meta_and_article(content):
    """
    Parse out the meta declarations (tags, keywords, etc...) from the article
    and return both.

    :param content: Contents of the article file
    :type content: str
    :returns: dict(meta) and str(article)
    """
    meta_field_names = FILTER_DEFAULTS.keys()
    meta = {}
    article = publish_doctree(content)
    for where, node in enumerate(article.traverse()):
        if node.tagname == 'field':
            label = str(node.children[0].children[0])
            if label in meta_field_names:
                value = str(node.children[1].children[0].children[0])
                meta[label] = value
                node.parent.remove(node)
        elif node.tagname == 'title' and 'title' not in meta:
            value = str(node.children[0])
            meta['title'] = value
            node.parent.remove(node)
    
    raw = parts_from_doctree(article)
    return (meta, raw['fragment'])