~dholbach/developer-ubuntu-com/rework-importer

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
from bs4 import BeautifulSoup
import codecs
import logging
import markdown
import os
import re
import sys

from . import (
    DEFAULT_LANG,
    SUPPORTED_ARTICLE_TYPES,
)
from .publish import get_or_create_page, slugify

if sys.version_info.major == 2:
    from urlparse import urlparse
else:
    from urllib.parse import urlparse


class Article:
    def __init__(self, fn, write_to):
        self.html = None
        self.page = None
        self.title = ""
        self.fn = fn
        self.write_to = slugify(self.fn)
        self.full_url = write_to
        self.slug = os.path.basename(self.full_url)

    def _find_local_images(self):
        '''Local images are currently not supported.'''
        soup = BeautifulSoup(self.html, 'html5lib')
        local_images = []
        for img in soup.find_all('img'):
            if img.has_attr('src'):
                (scheme, netloc, path, params, query, fragment) = \
                    urlparse(img.attrs['src'])
                if scheme not in ['http', 'https']:
                    local_images.extend([img.attrs['src']])
        return local_images

    def read(self):
        if os.path.splitext(self.fn)[1] not in SUPPORTED_ARTICLE_TYPES:
            logging.error("Don't know how to interpret '{}'.".format(
                self.fn))
            return False
        with codecs.open(self.fn, 'r', encoding='utf-8') as f:
            if self.fn.endswith('.md'):
                self.html = markdown.markdown(
                    f.read(),
                    output_format='html5',
                    extensions=['pymdownx.github'])
            elif self.fn.endswith('.html'):
                self.html = f.read()
        local_images = self._find_local_images()
        if local_images:
            logging.error('Found the following local image(s): {}'.format(
                ', '.join(local_images)
            ))
            return False
        self.title = self._read_title()
        self._remove_body_and_html_tags()
        self._use_developer_site_style()
        return True

    def _read_title(self):
        soup = BeautifulSoup(self.html, 'html5lib')
        if soup.title:
            return soup.title.text
        if soup.h1:
            return soup.h1.text
        return slugify(self.fn).replace('-', ' ').title()

    def _remove_body_and_html_tags(self):
        self.html = re.sub(r"<html>\n\s<body>\n", "", self.html,
                           flags=re.MULTILINE)
        self.html = re.sub(r"\s<\/body>\n<\/html>", "", self.html,
                           flags=re.MULTILINE)

    def _use_developer_site_style(self):
        begin = (u"<div class=\"row no-border\">"
                 "\n<div class=\"eight-col\">\n")
        end = u"</div>\n</div>"
        self.html = begin + self.html + end
        self.html = self.html.replace(
            "<pre><code>",
            "</div><div class=\"twelve-col\"><pre><code>")
        self.html = self.html.replace(
            "</code></pre>",
            "</code></pre></div><div class=\"eight-col\">")

    def replace_links(self, titles, url_map):
        soup = BeautifulSoup(self.html, 'html5lib')
        change = False
        for link in soup.find_all('a'):
            if not link.has_attr('class') or \
               'headeranchor-link' not in link.attrs['class']:
                for title in titles:
                    if title.endswith(link.attrs['href']) and \
                       link.attrs['href'] != url_map[title].full_url:
                        link.attrs['href'] = url_map[title].full_url
                        change = True
        if change:
            self.html = soup.prettify()
        return change

    def add_to_db(self):
        '''Publishes pages in their branch alias namespace.'''
        self.page = get_or_create_page(
            title=self.title, full_url=self.full_url, menu_title=self.title,
            html=self.html)
        if not self.page:
            return False
        self.full_url = self.page.get_absolute_url()
        return True

    def publish(self):
        if self.page.is_dirty(DEFAULT_LANG):
            self.page.publish(DEFAULT_LANG)
            self.page = self.page.get_public_object()
        return self.page


class SnappyArticle(Article):
    release_alias = None

    def read(self):
        if not Article.read(self):
            return False
        self.release_alias = re.findall(r'snappy/guides/(\S+?)/\S+?',
                                        self.full_url)[0]
        self._make_snappy_mods()
        return True

    def _make_snappy_mods(self):
        # Make sure the reader knows which documentation she is browsing
        if self.release_alias != 'current':
            before = (u"<div class=\"row no-border\">\n"
                      "<div class=\"eight-col\">\n")
            after = (u"<div class=\"row no-border\">\n"
                     "<div class=\"box pull-three three-col\">"
                     "<p>You are browsing the Snappy <code>%s</code> "
                     "documentation.</p>"
                     "<p><a href=\"/snappy/guides/current/%s\">"
                     "Back to the latest stable release &rsaquo;"
                     "</a></p></div>\n"
                     "<div class=\"eight-col\">\n") % (self.release_alias,
                                                       self.slug, )
            self.html = self.html.replace(before, after)

    def add_to_db(self):
        if self.release_alias == "current":
            # Add a guides/<page> redirect to guides/current/<page>
            page = get_or_create_page(
                title=self.title,
                full_url=self.full_url.replace('/current', ''),
                redirect="/snappy/guides/current/{}".format(self.slug))
            if not page:
                return False
        else:
            self.title += " (%s)" % (self.release_alias,)
        return Article.add_to_db(self)