Exemplo n.º 1
0
def generate_amp(article, template_path=AMP_TEMPLATE_PATH):
    """
    Generate AMP code for the target article.
    """

    configuration = get_configuration()
    if not article.nav_bar:
        parse_article(article)

    # If a vanilla HTML version exists, the canonical link should point to that.
    # Otherwise, the AMP article should link to itself.
    canonical_link = article.html_path if configuration.generate_vanilla_html else article.amp_path

    # Read in style sheet to embed in HTML page per the AMP specification.
    with configuration.style_sheet_path.open() as style_sheet_file:
        style_sheet = style_sheet_file.read()

    # Replace <img> tags with <amp-img> </amp-img> tags.
    matches = re.findall('<img.*?>', article.content)
    amp_content = article.content
    for match in matches:
        new_text = match.replace('<img', '<amp-img') + '</amp-img>'
        amp_content = amp_content.replace(match, new_text)

    # Now apply blog post template to article content.
    template = read_complete_file(template_path)
    html = template.format(nav_bar=article.nav_bar,
                           article_title=article.title,
                           article_content=amp_content,
                           last_updated=article.last_updated,
                           current_year=article.current_year,
                           blog_title=configuration.blog_title,
                           blog_subtitle=configuration.blog_subtitle,
                           owner=configuration.owner,
                           email_address=configuration.email_address,
                           rss_feed_path=construct_rss_url(configuration.root_url, configuration.rss_feed_path),
                           style_sheet=style_sheet,
                           root_url=configuration.root_url,
                           home_page_link='../',
                           description=article.description,
                           article_url=article.url,
                           article_image=article.first_image,
                           canonical_link=canonical_link,
                           schema_type="BlogPosting",
                           date_published=article.pub_date.strftime('%Y-%m-%d'))

    return html
Exemplo n.º 2
0
def generate_html_homepage(template_path=HTML_TEMPLATE_PATH):
    """
    Generate a vanilla HTML homepage that lists a preview of all blog articles
    in reverse chronological order.

    Args
      template_path: Path to the article template file.

    Return
      An HTML string of the full homepage code.

    """

    articles = create_article_previews()

    # Load blog article template from file.
    article_template = read_complete_file(template_path)

    # Combine article previews into one long aggregate post.
    preview_htmls = (generate_preview_html(article) for article in articles)
    aggregate_html = ''
    for preview_html in preview_htmls:
        aggregate_html += preview_html + '\n\n'

    last_updated = 'Last updated: ' + datetime.date.today().strftime('%B %d, %Y')
    current_year = datetime.date.today().strftime('%Y')
    configuration = get_configuration()

    # Apply blog article template to aggregate content.
    html_homepage = article_template.format(article_title=configuration.blog_title,
                                            nav_bar='',
                                            article_content=aggregate_html,
                                            last_updated=last_updated,
                                            current_year=current_year,
                                            blog_title=configuration.blog_title,
                                            blog_subtitle=configuration.blog_subtitle,
                                            owner=configuration.owner,
                                            email_address=configuration.email_address,
                                            rss_feed_path=configuration.rss_feed_path,
                                            style_sheet=configuration.style_sheet,
                                            root_url=configuration.root_url,
                                            home_page_link='',
                                            description=configuration.description,
                                            article_url=configuration.root_url,
                                            article_image='')

    return html_homepage
Exemplo n.º 3
0
def extract_article_preview(article):
    """
    Extract title, first photograph, and first paragraph from the target article.

    Args
      article: An instance of `file_tools.Article`.

    Return
      An instance of `ArticlePreview`.

    """

    html_path = article.html_path if article.html_path.exists() else article.amp_path
    article_text = read_complete_file(html_path)

    # Extract introductory text..
    paragraphs = article_text.split('<p>')
    intro_text_list = ['']
    for paragraph in paragraphs:
        if len(intro_text_list) > 2:
            break

        if paragraph.strip()[0] != '<':
            intro_text_list.append(paragraph)

    intro_text = '<p>'.join(intro_text_list)

    # Remove HTML tags from intro text..
    reverse_text = intro_text[::-1]
    # Add 4 here to account for the length of the string '</p>' in reverse.
    tag_index = reverse_text.index('>p/<') + 4
    intro_text = intro_text[:-tag_index]

    # Extract first photograph.
    match = re.search('<figure>.+?</figure>', article_text, re.DOTALL)
    if match:
        first_photo = match.group(0)

    else:
        first_photo = None

    article_preview = ArticlePreview(article, intro_text, first_photo)

    return article_preview
Exemplo n.º 4
0
def generate_html(article, template_path=HTML_TEMPLATE_PATH):
    """
    Apply blog post template to Markdown-to-HTML translation.

    Args
      article: An instance of file_tools.Article.
      template_path: Path to blog post template file. (Optional)

    Return
      Final blog post HTML string.

    """

    configuration = get_configuration()
    if not article.nav_bar:
        parse_article(article)

    rss_url = construct_rss_url(configuration.root_url, configuration.rss_feed_path)

    # Now apply blog post template to article content.
    template = read_complete_file(template_path)
    html = template.format(nav_bar=article.nav_bar,
                           article_title=article.title,
                           article_content=article.content,
                           last_updated=article.last_updated,
                           current_year=article.current_year,
                           blog_title=configuration.blog_title,
                           blog_subtitle=configuration.blog_subtitle,
                           owner=configuration.owner,
                           email_address=configuration.email_address,
                           rss_feed_path=rss_url,
                           style_sheet=configuration.style_sheet,
                           root_url=configuration.root_url,
                           home_page_link='../',
                           description=article.description,
                           article_url=article.url,
                           article_image=article.first_image)

    return html