From dbbd20c6dc78e25ef58df1e5997ff327bd0ca5ea Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Fri, 28 Apr 2017 19:06:22 +0200
Subject: Update to a new personalised version

---
 pyblog | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 222 insertions(+), 6 deletions(-)

(limited to 'pyblog')

diff --git a/pyblog b/pyblog
index 38a7c1d1..efd7711b 100755
--- a/pyblog
+++ b/pyblog
@@ -39,13 +39,13 @@ import lxml.etree as ET
 
 ############################# BLOG CONFIGURATIONS ##############################
 # Safe to customize
-BLOG_HOME = "http://archive.zhimingwang.org/"
-CUSTOM_DOMAIN = "archive.zhimingwang.org"  # GitHub Pages custom domain (could be None)
-BLOG_TITLE = "dl? cmplnts?"
-BLOG_DESCRIPTION = "Zhiming Wang's personal blog"
+BLOG_HOME = "http://neodarz.net/"
+CUSTOM_DOMAIN = "neodarz.net"  # GitHub Pages custom domain (could be None)
+BLOG_TITLE = "Why is there always a cat on whatever you're editing?"
+BLOG_DESCRIPTION = "Just my stupid personal website"
 LANGUAGE = "en-us"
-AUTHOR = "Zhiming Wang"
-AUTHOR_EMAIL = "zmwangx@gmail.com"
+AUTHOR = "neodarz"
+AUTHOR_EMAIL = "neodarz@neodarz.net"
 ATOM_ICON_PATH = "img/icon-400.png"  # set to None to leave it out
 RSS_ICON_PATH = "img/icon-100.png"  # set to None to leave it out
 RSS_ICON_WIDTH = 100
@@ -285,6 +285,189 @@ class RssItem(object):
             self.assemble_item()
         return ET.tostring(self.item).decode("utf-8")
 
+def generate_menu():
+    """Generate menu."""
+
+    sys.stderr.write("generating menu\n")
+
+    fd, tmppath = tempfile.mkstemp()
+    os.close(fd)
+
+    # Get the list of all html file in the build folder
+    html_fileList = []
+    for root, dirs, files in os.walk(BUILDDIR):
+        for name in files:
+            if name.endswith(".html"):
+                try:
+                    html_fileList.append(os.path.join(root.split('build/')[1], name))
+                except IndexError:
+                    html_fileList.append(name)
+
+    # Generate the a string who contain a list of file in website folder
+    htmly_website_page = "<ul>"
+    for name in os.listdir(os.path.join(BUILDDIR, "website")):
+        htmly_website_page += "<li><a href='/website/"+name+"'>"+name.split('.html')[0]+"</a></li>"
+    htmly_website_page += "</ul>"
+
+    # Write the menu in file, in place of the <generated_menu>
+    for html_file in html_fileList:
+        with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+            if os.path.exists("build/"+html_file):
+                with open("build/"+html_file, 'r', encoding='utf-8') as indexmd:
+                    lines = indexmd.readlines()
+                    with open("build/"+html_file, 'w', encoding='utf-8') as indexmd:
+                        for line in lines:
+                            indexmd.write(re.sub(r'-- generate menu here --', htmly_website_page, line))
+
+        os.remove(tmppath)
+
+def generate_blog_list(feed):
+    """"Generate blog list """
+
+    sys.stderr.write("generating blog list\n")
+
+    html_fileList = []
+    for root, dirs, files in os.walk(BUILDDIR):
+        for name in files:
+            if re.search(r'blog',root):
+                if name.endswith(".html"):
+                    try:
+                        html_fileList.append(os.path.join(root.split('blog/')[1], name))
+                    except IndexError:
+                        html_fileList.append(name)
+
+    # generate TOC
+    for html_file in html_fileList:
+        div_blog_list = u'<div class="blog-index" id="toc">\n</table>\n'
+        year = 10000  # will be larger than the latest year for quite a while
+        # recall that entries are in reverse chronological order
+        table_opened = False
+        for entry in feed.entries:
+            date = entry.updated_datetime
+            if date.year < year:
+                # close the previous table if there is one
+                if table_opened:
+                    div_blog_list += u'</table>\n'
+                # write a new <h2 class="blog-index-year-title"> tag with the smaller year
+                year = date.year
+                div_blog_list += u'\n<h2 class="blog-index-year-title" id="{0}">{0}</h2>\n\n'.format(year)
+                div_blog_list += u'<table class="blog-index-yearly-index">\n'
+                table_opened = True
+
+            # write a new table row entry in Markdown, in the format:
+            #
+            #   <tr>
+            #     <td class="blog-index-post-date"><time class="date" datetime="2015-05-05T00:06:04-0700">May 5</time></td>
+            #     <td class="blog-index-post-title">[Blah blah](/blog/2015-05-04-blah-blah.html)</td>
+            #   </tr>
+            monthday = date.strftime("%b %d")
+            div_blog_list += (u'<tr><td class="blog-index-post-date"><time class="date" datetime="%s">%s</time></td>'
+                          '<td class="blog-index-post-title"><a href="%s">%s</a></td></tr>\n' %
+                          (date.isoformat(), monthday, entry.relpath, entry.title_text))
+        if table_opened:
+            div_blog_list += u'</table>\n'
+        div_blog_list += u'</div>'
+
+        fd, tmppath = tempfile.mkstemp()
+        os.close(fd)
+        with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+            if os.path.exists("build/blog/index.html"):
+                with open("build/blog/index.html", 'r', encoding='utf-8') as indexmd:
+                    lines = indexmd.readlines()
+                    with open("build/blog/index.html", 'w', encoding='utf-8') as indexmd:
+                        for line in lines:
+                            indexmd.write(re.sub(r'-- generate blog_list here --', div_blog_list, line))
+
+
+def generate_notes_list():
+    """"Generate notes list """
+
+    sys.stderr.write("generating notes list\n")
+
+    html_fileList = []
+    for root, dirs, files in os.walk(BUILDDIR):
+        for name in files:
+            if re.search(r'notes',root):
+                if name.endswith(".html"):
+                    try:
+                        html_fileList.append(os.path.join(root.split('notes/')[1], name))
+                    except IndexError:
+                        html_fileList.append(name)
+
+    div_notes_list = u'<div class="blog-index" id="toc">\n</table>\n'
+    year = 10000  # will be larger than the latest year for quite a while
+    # recall that entries are in reverse chronological order
+    table_opened = False
+    for name in os.listdir(os.path.join(BUILDDIR, "notes")):
+        if re.match(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}.*\.html", name):
+            htmlpath = os.path.join(BUILDDIR, "notes", name)
+            #tentry = AtomEntry()
+            #item = RssItem()
+            try:
+                with open(htmlpath, encoding="utf-8") as htmlfile:
+                    soup = bs4.BeautifulSoup(htmlfile.read(), "lxml")
+
+                    # generate atom entry
+                    #entry.author = copy.deepcopy(feed.author)  # assume it's always the same author
+                    #entry_url = urllib.parse.urljoin(BLOG_HOME, "blog/%s" % name)
+                    #entry.id_text = entry_url
+                    #entry.id = ET.Element("id")
+                    #entry.id.text = entry_url
+                    relpath = "/notes/%s" % name
+
+                    #entry.link = ET.Element("link", href=entry_url)
+                    title_text = soup.title.text
+
+                    #entry.title = ET.Element("title", type="html")
+                    #entry.title.text = entry.title_text
+                    post_date = soup.find("meta", attrs={"name": "date"})["content"]
+                    updated_datetime = dateutil.parser.parse(post_date)
+
+                    date = updated_datetime
+                    if date.year < year:
+                        # close the previous table if there is one
+                        if table_opened:
+                            div_notes_list += u'</table>\n'
+                        # write a new <h2 class="blog-index-year-title"> tag with the smaller year
+                        year = date.year
+                        div_notes_list += u'\n<h2 class="blog-index-year-title" id="{0}">{0}</h2>\n\n'.format(year)
+                        div_notes_list += u'<table class="blog-index-yearly-index">\n'
+                        table_opened = True
+
+                    # write a new table row entry in Markdown, in the format:
+                    #
+                    #   <tr>
+                    #     <td class="blog-index-post-date"><time class="date" datetime="2015-05-05T00:06:04-0700">May 5</time></td>
+                    #     <td class="blog-index-post-title">[Blah blah](/blog/2015-05-04-blah-blah.html)</td>
+                    #   </tr>
+                    monthday = date.strftime("%b %d")
+                    div_notes_list += (u'<tr><td class="blog-index-post-date"><time class="date" datetime="%s">%s</time></td>'
+                                  '<td class="blog-index-post-title"><a href="%s">%s</a></td></tr>\n' %
+                                  (date.isoformat(), monthday, relpath, title_text))
+
+            except Exception:
+                sys.stderr.write("error: failed to generate feed entry from %s\n" % name)
+                with open(htmlpath, encoding="utf-8") as htmlfile:
+                    sys.stderr.write("dumping HTML:%s\n\n" % htmlfile.read())
+                raise
+
+    if table_opened:
+        div_notes_list += u'</table>\n'
+    div_notes_list += u'</div>'
+
+    fd, tmppath = tempfile.mkstemp()
+    os.close(fd)
+    with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+        if os.path.exists("build/notes/index.html"):
+            with open("build/notes/index.html", 'r', encoding='utf-8') as indexmd:
+                lines = indexmd.readlines()
+                with open("build/notes/index.html", 'w', encoding='utf-8') as indexmd:
+                    for line in lines:
+                        indexmd.write(re.sub(r'-- generate notes_list here --', div_notes_list, line))
+
+
+
+
 
 def generate_index(feed):
     """Generate index.html from index.md and a TOC."""
@@ -415,6 +598,35 @@ def generate_sitemap(feed):
                           ET.tostring(sitemap).decode('utf-8'))
         sys.stderr.write("wrote sitemap.xml\n")
 
+def overide_title():
+    """Override the title of some page for a better render"""
+    sys.stderr.write("Overriding some titles\n")
+
+    filenames =['build/index.html',
+                'build/blog/index.html',
+                'build/notes/index.html']
+
+    for root, dirs, files in os.walk(BUILDDIR):
+        for name in files:
+            if re.search(r'website($)',root):
+                if name.endswith(".html"):
+                    try:
+                        filenames.append("build"+os.path.join(root.split(BUILDDIR)[1], name))
+                    except IndexError:
+                        filenames.append(name)
+
+    fd, tmppath = tempfile.mkstemp()
+    os.close(fd)
+    for filename in filenames:
+        print (filename)
+        with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+            if os.path.exists(filename):
+                with open(filename, 'r', encoding='utf-8') as indexmd:
+                    lines = indexmd.readlines()
+                    with open(filename, 'w', encoding='utf-8') as indexmd:
+                        for line in lines:
+                            indexmd.write(re.sub(r'\<h1 class="article-title"\>(.+?)\<\/h1\>', "<h1 class='article-title'>Why is there always a cat on whatever you're editing?</h1>", line))
+
 
 def absolutify_links(soup, baseurl):
     """Make links in an article absolute.
@@ -569,6 +781,10 @@ def generate_index_and_feed():
     rss.items.sort(key=lambda item: item.timestamp, reverse=True)
 
     generate_index(feed)
+    generate_menu()
+    generate_blog_list(feed)
+    generate_notes_list()
+    overide_title()
 
     feed.updated_datetime = current_datetime()
     feed.updated = ET.Element("updated")
-- 
cgit v1.2.1