Exemplo n.º 1
0
def generate_post_meta(date, id):
    post_meta_date = Html.generate_element_by_str('div',
                                                  date,
                                                  class_name='post_meta_date')
    return Html.generate_element_by_str('div',
                                        post_meta_date,
                                        class_name='post_meta')
Exemplo n.º 2
0
def generate_tag_ul(tag_info_arr):
    li_arr = []
    for tag_info in tag_info_arr:
        button_content = tag_info['name'] + '(' + str(tag_info['count']) + ')'
        button = Html.generate_element_by_str('button', button_content)
        li = Html.generate_element_by_str('li', button)
        li_arr.append(li)
    return Html.generate_element_by_strs('ul', li_arr, id='tags_ul')
Exemplo n.º 3
0
def generate_post_article(title, date, id, text):
    post_meta = generate_post_meta(date, id)
    h1_a = Html.generate_element_by_str('a',
                                        title,
                                        href=const.POST_URL_PREFIX + id +
                                        '/index.html',
                                        title=title)
    h1 = Html.generate_element_by_str('h1', h1_a)
    return Html.generate_element_by_strs('article', [post_meta, h1, text],
                                         class_name='post')
Exemplo n.º 4
0
    def generate_complete_html(cls, page_info, main_content=''):
        page_info = cls.__check_make_page_info(page_info)

        with open(const.GLOBAL_CONF_FILE_PATH, 'r', encoding='utf-8') as fd:
            global_conf = json.load(fd)

        start_css_arr = global_conf['start_css_arr'] + \
            page_info['start_css_arr']
        start_js_arr = global_conf['start_js_arr'] + page_info['start_js_arr']
        end_css_arr = global_conf['end_css_arr'] + page_info['end_css_arr']
        end_js_arr = global_conf['end_js_arr'] + page_info['end_js_arr']

        head = cls.__generate_head(page_info['title'], page_info['key_words'],
                                   page_info['description'], start_css_arr,
                                   start_js_arr)

        with open(const.HEADER_CONF_FILE_PATH, 'r', encoding='utf-8') as fd:
            conf = json.load(fd)
            header = Component.generate_header(conf)

        with open(const.SIDEBAR_CONF_FILE_PATH, 'r', encoding='utf-8') as fd:
            conf = json.load(fd)
            sidebar = Component.generate_sidebar(conf)

        main = cls.__generate_main(main_content)

        footer = cls.__generate_footer(global_conf['footer'], end_css_arr,
                                       end_js_arr)
        html_str = ' '.join([head, header, sidebar, main, footer])
        return Html.prettify(html_str)
Exemplo n.º 5
0
def generate_post_item(info):
    date = util.datetime2date(info['create_datetime'])
    preface_file_name = os.path.join(const.POST_DIR_PATH, info['id'],
                                     const.POST_PREFACE_FILE_PATH)
    with open(preface_file_name, 'r', encoding='utf-8') as fd:
        text = Html.from_markdown_str(fd.read())
    return generate_post_article(info['title'], date, info['id'], text)
Exemplo n.º 6
0
def generate_tag_contents_ul(tag_posts_arr):
    li_arr = []
    for tag_posts in tag_posts_arr:
        tag = tag_posts['name']
        h2 = Html.generate_element_by_str('h2', tag)

        sub_li_arr = []
        posts = tag_posts['posts']
        for post in posts:
            if 'create_datetime' in post:
                date = blog.util.datetime2date(post['create_datetime'])
                span = Html.generate_element_by_str('span', date)
                a_content = post['title'] + ' ' + span
            else:
                a_content = post['title']
            a = Html.generate_element_by_str('a',
                                             a_content,
                                             href=post['link'],
                                             title=post['title'])
            sub_li = Html.generate_element_by_str('li', a)
            sub_li_arr.append(sub_li)
        sub_ul = Html.generate_element_by_strs('ul', sub_li_arr)
        li = Html.generate_element_by_strs('li', [h2, sub_ul])
        li_arr.append(li)
    return Html.generate_element_by_strs('ul', li_arr, id='tag_contents_ul')
Exemplo n.º 7
0
    def __generate_footer(cls, footer_html, css_arr, js_arr):
        css_str = ''
        for css in css_arr:
            css_str += Html.generate_css(css)

        js_str = ''
        for js in js_arr:
            js_str += Html.generate_js(js)

        return '''
                <footer>
                    %s
                </footer>
            </div>
            %s
            %s
        </body>
        </html>
        ''' % (footer_html, css_str, js_str)
 def generate_by_file(cls,
                      file_prefix,
                      file_dir_name=const.GENERAL_PAGES_DIR_PATH):
     md_file_path = os.path.join(file_dir_name, str(file_prefix) + '.md')
     with open(md_file_path, 'r', encoding='utf-8') as fd:
         main = Html.from_markdown_str(fd.read())
     cond_file_path = os.path.join(file_dir_name,
                                   str(file_prefix) + '.conf')
     with open(cond_file_path, 'r', encoding='utf-8') as fd:
         page_info = json.load(fd)
     return Page.generate_complete_html(page_info, main)
Exemplo n.º 9
0
def generate_archive_page():
    h1 = Html.generate_element_by_str('h1', '归档')
    tag_posts_arr = generate_archive_data()
    tag_contents_ul = special_pages.util.generate_tag_contents_ul(
        tag_posts_arr)
    main_content = ' '.join([h1, tag_contents_ul])
    conf_file_path = os.path.join(const.SPECAIL_PAGES_DIR_PATH,
                                  ARCHIVE_FILE_PATH_PREFIX + '.conf')
    with open(conf_file_path, 'r', encoding='utf-8') as fd:
        page_conf = json.load(fd)
    return Page.generate_complete_html(page_conf, main_content)
Exemplo n.º 10
0
def generate_sitemap():
    urlset_str = ''

    for static_html in STATIC_HTML_ARR:
        file_path = os.path.join(const.PUBLIC_PATH, static_html)
        mtime = os.path.getmtime(file_path)
        lastmod = time.strftime('%Y-%m-%d', time.localtime(mtime))
        loc = const.DOMAIN_ROOT_URL_PREFIX + '/' + static_html
        changefreq = 'hourly'
        priority = '1.0'
        urlset_str += _generate_url(loc, lastmod, changefreq, priority)

    for dir_name in HTML_DIR_ARR:
        real_dir = os.path.join(const.PUBLIC_PATH, dir_name)
        for item in os.listdir(real_dir):
            if item.endswith('.html'):
                file_path = os.path.join(real_dir, item)
                mtime = os.path.getmtime(file_path)
                lastmod = time.strftime('%Y-%m-%d', time.localtime(mtime))
                loc = const.DOMAIN_ROOT_URL_PREFIX + '/' + dir_name + '/' + item
                changefreq = 'hourly'
                priority = '0.9'
                urlset_str += _generate_url(loc, lastmod, changefreq, priority)

    with open(const.POST_JSON_FILE_PATH, 'r', encoding='utf-8') as fd:
        posts = json.load(fd)['posts']
        for post in posts:
            lastmod = post['update_datetime'][0:post['update_datetime'].
                                              index(' ')]
            loc = const.DOMAIN_ROOT_URL_PREFIX + post['link']
            changefreq = 'daily'
            priority = '0.9'
            urlset_str += _generate_url(loc, lastmod, changefreq, priority)

    urlset = Html.generate_element_by_str(
        'urlset',
        urlset_str,
        xmlns="http://www.google.com/schemas/sitemap/0.9")
    sitemap_str = '''<?xml version="1.0" encoding="utf-8"?>%s''' % (urlset)
    return Html.prettify(sitemap_str)
Exemplo n.º 11
0
def generate_post_end(tags, create_datetime, update_datetime):
    tag_a_arr = []
    for tag in tags:
        a_href = '/tag.html?tag=' + tag
        a_content = '#' + tag
        a = Html.generate_element_by_str('a',
                                         a_content,
                                         href=a_href,
                                         title=tag)
        tag_a_arr.append(a)
    tag_str = ' '.join(tag_a_arr)
    post_tags = Html.generate_element_by_str('div',
                                             '标签:' + tag_str,
                                             class_name='post_tags')

    create_datetime_ele = Html.generate_element_by_str(
        'p', '发表时间:' + create_datetime)
    if create_datetime == update_datetime:
        return Html.generate_element_by_str('div',
                                            '<br />--END--<br />' +
                                            create_datetime_ele + post_tags,
                                            class_name='post_end')
    else:
        update_datetime_ele = Html.generate_element_by_str(
            'p', '最后修改:' + update_datetime)
        return Html.generate_element_by_str(
            'div',
            '<br />--END--<br />' + create_datetime_ele + update_datetime_ele +
            post_tags,
            class_name='post_end')
Exemplo n.º 12
0
def generate_page_file(post_arr, index, has_former, has_next):
    post_li_arr = []
    for post in post_arr:
        post_item = generate_post_item(post)
        li = Html.generate_element_by_str('li', post_item)
        post_li_arr.append(li)
    if len(post_li_arr) == 0:
        post_catlog = Html.generate_element_by_str('ul',
                                                   '当前暂无帖子',
                                                   class_name='post_catlog')
    else:
        post_catlog = Html.generate_element_by_strs('ul',
                                                    post_li_arr,
                                                    class_name='post_catlog')

    pagination = generate_page_pagination(index, has_former, has_next)
    main_html = ' '.join([post_catlog, pagination])

    page_conf = {
        "start_css_arr": [
            "https://xiaolisong.github.io/blog/css/article.css",
            "https://xiaolisong.github.io/blog/css/page.css"
        ],
        "description":
        "导航页, 第%d页" % (index)
    }
    page_file_name = os.path.join(const.PUBLIC_PATH, PAGE_DIR_NAME,
                                  str(index) + '.html')
    with open(page_file_name, 'w+', encoding='utf-8') as fd:
        html = Page.generate_complete_html(page_conf, main_html)
        fd.write(html)
        print("生成 page 成功,页号:%d" % (index))

    # 生成首页
    if index == 0:
        file_name = os.path.join(const.PUBLIC_PATH, 'index.html')
        with open(file_name, 'w+', encoding='utf-8') as fd:
            html = Page.generate_complete_html(page_conf, main_html)
            fd.write(html)
            print("生成 首页 成功")
Exemplo n.º 13
0
def generate_page_pagination(index, has_former, has_next):
    if has_former:
        href = const.ROOT_URL_PREFIX + PAGE_DIR_NAME + '/' + str(index -
                                                                 1) + '.html'
        former_a = Html.generate_element_by_str('a',
                                                '<-上一页',
                                                href=href,
                                                class_name='former_link',
                                                title='<-上一页')
    else:
        former_a = Html.generate_element_by_str('a',
                                                href='#',
                                                class_name='former_link',
                                                visibility='hiddern')

    if has_next:
        href = const.ROOT_URL_PREFIX + PAGE_DIR_NAME + '/' + str(index +
                                                                 1) + '.html'
        next_a = Html.generate_element_by_str('a',
                                              '下一页->',
                                              href=href,
                                              class_name='former_link',
                                              title='下一页->')
    else:
        next_a = Html.generate_element_by_str('a',
                                              href='#',
                                              class_name='next_link',
                                              visibility='hiddern')

    middle_a = Html.generate_element_by_str('a',
                                            '-归档-',
                                            href='/archive.html',
                                            class_name='center_link')
    return Html.generate_element_by_strs('nav', [former_a, next_a, middle_a],
                                         class_name='pagination')
Exemplo n.º 14
0
    def __generate_head(cls, title, key_words, description, css_arr, js_arr):
        '''
        '''
        css_str = ''
        for css in css_arr:
            css_str += Html.generate_css(css)

        js_str = ''
        for js in js_arr:
            js_str += Html.generate_js(js)

        return '''<html>
            <head>
            <title>%s</title>
            <meta name="keywords" content="%s">
            <meta name="description" content="%s">
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
            %s
            %s
            </head>
        <body>
        ''' % (title, key_words, description, css_str, js_str)
Exemplo n.º 15
0
def generate_post(post_id):
    post_dir_name = os.path.join(const.POST_DIR_PATH, post_id)
    conf_file_name = os.path.join(post_dir_name, const.POST_CONF_FILE_PATH)
    with open(conf_file_name, 'r', encoding='utf-8') as fd:
        post_conf = json.load(fd)
        md_file_name = os.path.join(post_dir_name, const.POST_MD_FILE_PATH)
        with open(md_file_name, 'r', encoding='utf-8') as fd:
            text = Html.from_markdown_str(fd.read())
            create_time, update_time = util.get_post_create_update_datetime(
                post_id)
            main = generate_post_main(post_conf['title'], create_time,
                                      update_time, post_id, text,
                                      post_conf['tags'])
            return Page.generate_complete_html(post_conf, main)
Exemplo n.º 16
0
def generate_rss_item(title, link, description, pubDate):
    title = Html.generate_element_by_str('title', title)
    link = Html.generate_element_by_str('link', link)
    description = Html.generate_element_by_str('description', description)
    pubDate = Html.generate_element_by_str('pubDate', pubDate)
    guid = Html.generate_element_by_str('guid', link)
    item = title + link + description + pubDate + guid
    item = Html.generate_element_by_str('item', item)
    return item
Exemplo n.º 17
0
def generate_recommend_page():
    with open(RECOMMEND_JSON_FILE_PATH, 'r', encoding='utf-8') as fd:
        posts = json.load(fd)['posts']
    tag_info_arr, tag_posts_arr = special_pages.util.generate_tag_data(posts)

    h1 = Html.generate_element_by_str('h1', '推荐')
    tag_ul = special_pages.util.generate_tag_ul(tag_info_arr)
    tag_contents_ul = special_pages.util.generate_tag_contents_ul(
        tag_posts_arr)
    main_content = ' '.join([h1, tag_ul, tag_contents_ul])
    conf_file_path = os.path.join(const.SPECAIL_PAGES_DIR_PATH,
                                  RECOMMEND_FILE_PATH_PREFIX + '.conf')
    with open(conf_file_path, 'r', encoding='utf-8') as fd:
        page_conf = json.load(fd)
    return Page.generate_complete_html(page_conf, main_content)
Exemplo n.º 18
0
def generate_rss():
    with open(const.POST_JSON_FILE_PATH, 'r', encoding='utf-8') as fd:
        posts = json.load(fd)['posts'][0:const.POST_CNT_PER_PAGE]
    item_str = ''
    for post in posts:
        title = post['title']
        link = post['link']
        description_file_name = os.path.join(const.POST_DIR_PATH, post['id'],
                                             const.POST_PREFACE_FILE_PATH)
        with open(description_file_name, 'r', encoding='utf-8') as fd:
            description = Html.from_markdown_str(fd.read())
        pubDate = post['update_datetime']
        item_str += generate_rss_item(title, link, description, pubDate)

    title = Html.generate_element_by_str('title', const.BLOG_NAME)
    link = Html.generate_element_by_str('link', const.DOMAIN_ROOT_URL_PREFIX)
    description = Html.generate_element_by_str('description',
                                               const.BLOG_DESCRIPTION)
    atom_link = Html.generate_sp_element('atom:link',
                                         href=const.DOMAIN_ROOT_URL_PREFIX +
                                         '/' + RSS_FILE_PATH,
                                         rel='self')
    language = Html.generate_element_by_str('language', const.LANGUAGE)
    lastBuildDate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    lastBuildDate = Html.generate_element_by_str('lastBuildDate',
                                                 lastBuildDate)
    channel = Html.generate_element_by_strs(
        'channel',
        [title, link, description, atom_link, language, lastBuildDate])

    rss_str = '''<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    %s
    %s
</rss>
''' % (channel, item_str)
    return Html.prettify(rss_str)