aboutsummaryrefslogtreecommitdiff
path: root/utils/utils.py
blob: f8a756c50711f11636186b98816d938d010d4f10 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/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

import http.server
import multiprocessing
import os
import sys

from config.config import *
from rss import *

@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

def link_img_tags(soup):
    """Convert each <img> tag in <article> to a link to its original."""
    if not soup.article:
        return
    for img_tag in soup.article.find_all("img"):
        a_tag = soup.new_tag("a", href=img_tag["src"], target="_blank")
        a_tag.insert(0, copy.copy(img_tag))
        img_tag.replace_with(a_tag)


def _pre_tag_insert_line_numbers(soup, pre_tag):
    """Insert line numbers to a pre tag."""
    num_lines = len(pre_tag.text.split("\n"))
    for line_number in range(1, num_lines + 1):
        # line number divs will look like:
        # <span class="line-number" data-line="1" style="top: 0em"><!----></span>
        # <span class="line-number" data-line="2" style="top: 1.35em"><!----></span>
        ln_tag = soup.new_tag("span")
        ln_tag["class"] = "line-number"
        ln_tag["data-line"] = line_number
        ln_tag["style"] = "top: %.2fem" % ((line_number - 1) * 1.35)
        # add a comment to the content of the span to suppress tidy5
        # empty <span> tag warning
        ln_tag.append(soup.new_string("", bs4.Comment))
        pre_tag.code.append(ln_tag)


def process_footnote_backlinks(soup):
    """Add class attribute "footnotes-backlink" to each footnote backlink."""
    for footnotes in soup.find_all("div", attrs={"class": "footnotes"}):
        for fn_a_tag in footnotes.find_all(lambda tag:
                                           tag.name == "a" and
                                           tag.has_attr("href") and
                                           tag["href"].startswith("#fnref") and
                                           tag.string == "\u21A9"):  # U+21A9: LEFTWARDS ARROW WITH HOOK
            fn_a_tag["class"] = "footnotes-backlink"
            fn_a_tag.string = "\u21A9\uFE0E" # U+FE0E: VARIATION SELECTOR-15


def postprocess_html_file(htmlfilepath):
    """Perform a series of postprocessing to an HTML file."""
    with open(htmlfilepath, "r+", encoding="utf-8") as htmlfileobj:
        soup = bs4.BeautifulSoup(htmlfileobj.read(), "lxml")

        # a series of postprocessing (extensible)
        process_image_sizes(soup)
        link_img_tags(soup)
        process_footnote_backlinks(soup)

        # write back
        htmlfileobj.seek(0)
        htmlfileobj.write(str(soup))
        htmlfileobj.truncate()


def static_vars(**kwargs):
    def decorate(func):
        for k in kwargs:
            setattr(func, k, kwargs[k])
        return func
    return decorate


def sanitize(string):
    """Sanitize string (title) for URI consumption."""
    if isinstance(string, bytes):
        string = string.decode('utf-8')
    # to lowercase
    string = string.lower()
    # strip all non-word, non-hyphen and non-whitespace characters
    string = re.sub(r"[^\w\s-]", "", string)
    # replace consecutive whitespaces with a single hyphen
    string = re.sub(r"\s+", "-", string)
    # percent encode the result
    return urllib.parse.quote(string)


class HTTPServerProcess(multiprocessing.Process):
    """This class can be used to run an HTTP server."""

    def __init__(self, rootdir):
        """Initialize the HTTPServerProcess class.

        Parameters
        ----------
        rootdir : str
            The root directory to serve from.

        """

        super().__init__()
        self.rootdir = rootdir

    def run(self):
        """Create an HTTP server and serve forever.

        Runs on localhost. The default port is 8000; if it is not
        available, a random port is used instead.
        """

        os.chdir(self.rootdir)
        # pylint: disable=invalid-name
        HandlerClass = http.server.SimpleHTTPRequestHandler
        try:
            httpd = http.server.HTTPServer(("", 8001), HandlerClass)
        except OSError:
            httpd = http.server.HTTPServer(("", 0), HandlerClass)
        _, portnumber = httpd.socket.getsockname()
        sys.stderr.write("server serving on http://localhost:%d\n" % portnumber)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()


def list_posts():
    """List all posts, with date, title, and path to source file.

    This function only lists posts that has been built (since it reads
    metadata from HTML rather than Markdown).

    Returns
    -------
    posts : list
        A list of posts, in reverse chronological order, where each
        element is a tuple of (date, title, path to source file).

    """
    posts = []
    for name in os.listdir(os.path.join(BUILDDIR, "blog")):
        if not re.match(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}.*\.html", name):
            continue

        htmlpath = os.path.join(BUILDDIR, "blog", name)
        entry = AtomEntry()
        item = RssItem()
        try:
            with open(htmlpath, encoding="utf-8") as htmlfile:
                soup = bs4.BeautifulSoup(htmlfile.read(), "lxml")
                title = soup.title.text
                date = dateutil.parser.parse(soup.find("meta", attrs={"name": "date"})["content"])
                source_path = os.path.join(POSTSDIR, re.sub(r'.html$', '.md', name))
                posts.append((date, title, source_path))
        except Exception:
            sys.stderr.write("error: failed to read metadata from HTML file %s\n" % name)
            with open(htmlpath, encoding="utf-8") as htmlfile:
                sys.stderr.write("dumping HTML:%s\n\n" % htmlfile.read())
            raise

    posts.sort(key=lambda post: post[0], reverse=True)
    return posts