aboutsummaryrefslogtreecommitdiff
path: root/utils/utils.py
blob: eec10adf6358766fca8a45864e7f768fc604765d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/python3

from contextlib import contextmanager
import colorama

import time
import datetime
import dateutil.tz

import bs4
import urllib.parse

import re

import lxml.etree as ET


@contextmanager
def init_colorama():
    """Set global foreground modifying ANSI codes.

    BLACK, BLUE, CYAN, GREEN, MAGENTA, RED, WHITE, YELLOW, and RESET.

    """

    # pylint: disable=exec-used,invalid-name

    colorama.init()
    for color, ansi in colorama.Fore.__dict__.items():
        exec("global {0}; {0} = '{1}'".format(color, ansi))
    yield
    for color in colorama.Fore.__dict__:
        exec("global {0}; {0} = ''".format(color))
    colorama.deinit()


def current_datetime():
    """Return the current datetime, complete with tzinfo.

    Precision is one second. Timezone is the local timezone.
    """
    return datetime.datetime.fromtimestamp(round(time.time()),
                                           dateutil.tz.tzlocal())


def absolutify_links(soup, baseurl):
    """Make links in an article absolute.

    Parameters
    ----------
    soup : bs4.BeautifulSoup
    baseurl : str

    """
    for tag in soup.find_all(lambda tag: tag.has_attr("href")):
        tag["href"] = urllib.parse.urljoin(baseurl, tag["href"])
    for tag in soup.find_all(lambda tag: tag.has_attr("src")):
        tag["src"] = urllib.parse.urljoin(baseurl, tag["src"])

# MARKDOWN EXTENSION!
#
# See docstring of process_image_sizes for documentation.

# If matched, 1st group is width, 3rd group (optional) is height, and
# 4th group is actual text.
IMAGESIZE_EXTRACTOR = re.compile(r'\|(\d+)(x(\d+))?\|\s*(.*)')

def process_image_sizes(soup):
    """Process the image size Markdown extension.

    Allows specifying image size in a Markdown image construct
    ![](). The syntax is:

        ![|width(xheight)?| alt](src)

    where width and height are positive integers (xheight is optional),
    and alt is the regular alt string (either plain or with some
    Markdown formatting). alt string, as usual, is optional.

    Examples:

        ![|1920x1080| Hello, world!](http://example.com/hello.png)
        ![|1920| *Hey!*](http://example.com/hey.png)
        ![|1280x800|](http://example.com/noalt.png)

    """
    if not soup.article:
        return
    for img_tag in soup.article.find_all("img"):
        if img_tag.has_attr("alt"):
            match = IMAGESIZE_EXTRACTOR.match(img_tag["alt"])
            if match:
                width, _, height, realalt = match.groups()
                img_tag["width"] = width
                if height:
                    img_tag["height"] = height
                img_tag["alt"] = realalt

    # strip image specs from captions, if any
    for caption in soup.article.select(".figure .caption"):
        if hasattr(caption, "contents") and isinstance(caption.contents[0], str):
            match = IMAGESIZE_EXTRACTOR.match(caption.contents[0])
            if match:
                caption.contents[0].replace_with(match.group(4))


def make_sitemap_url_element(link, updated=None, changefreq=None, priority=None):
    """Make a sitemap <url> element.

    Parameters
    ----------
    link : str or xml.etree.ElementTree.Element
        If using an xml.etree.ElementTree.Element element, then it shall
        be an atom:link element, e.g., <link href="http://zmwangx.github.io/"/>.
    updated : datetime or xml.etree.ElementTree.Element, optional
        If using an xml.etree.ElementTree.Element element, then it shall
        be an atom:updated element, e.g.,
        <updated>2015-05-05T22:38:42-07:00</updated>.
    changefreq : {"always", "hourly", "daily", "weekly", "monthly", "yearly", "never"}, optional
    priority : {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}, optional

    """

    urlelem = ET.Element("url")
    loc = ET.Element("loc")
    loc.text = link.attrib["href"] if isinstance(link, ET._Element) else link
    urlelem.append(loc)
    if updated is not None:
        lastmod = ET.Element("lastmod")
        lastmod.text = (updated.text if isinstance(updated, ET._Element)
                        else updated.isoformat())
        urlelem.append(lastmod)
    if changefreq is not None:
        changefreq_elem = ET.Element("changefreq")
        changefreq_elem.text = changefreq
        urlelem.append(changefreq_elem)
    if priority is not None:
        priority_elem = ET.Element("priority")
        priority_elem.text = "%.1f" % priority
        urlelem.append(priority_elem)
    return urlelem