Skip to content

Commit

Permalink
update makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
dothinking committed Mar 19, 2021
2 parents 188118d + a2086d6 commit 3a7ad7e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
23 changes: 15 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
TOPDIR :=$(shell pwd)
DOCS :=$(TOPDIR)/docs
BUILD :=$(TOPDIR)/build
BUILDDOCS :=$(BUILD)/docs

# mkdocs configuration file
MKDOCSCFG :=_mkdocs.yml
SERVECFG :=$(TOPDIR)/$(MKDOCSCFG)
BUILDCFG :=$(BUILD)/$(MKDOCSCFG)

# sub-dir names for categoried posts
CATEGORIES :=categories
YEARS :=years

# count of items show in navigation bar
NAVCNT :=5
INDEXCNT :=5
SERVECFG :=$(TOPDIR)/$(MKDOCSCFG)
BUILDCFG :=$(BUILD)/$(MKDOCSCFG)

# count of latest posts show in homepage
INDEXCNT :=7



Expand All @@ -35,16 +42,16 @@ copy: pre_process
.PHONY: serve
serve: pre_process
@echo Summarizing pages...
@cat mkdocs.yml > "$(SERVECFG)"
@python run.py serve "$(DOCS)" $(YEARS) $(CATEGORIES) $(NAVCNT) $(INDEXCNT) >> "$(SERVECFG)"
@cp mkdocs.yml "$(SERVECFG)"
@python run.py serve "$(SERVECFG)" $(YEARS) $(CATEGORIES) $(NAVCNT) $(INDEXCNT)
@mkdocs serve -f "$(SERVECFG)"


.PHONY: build
build: copy
@echo Summarizing pages...
@cat mkdocs.yml > "$(BUILDCFG)"
@python run.py build "$(BUILDDOCS)" $(YEARS) $(CATEGORIES) $(NAVCNT) $(INDEXCNT) >> "$(BUILDCFG)"
@cp mkdocs.yml "$(BUILDCFG)"
@python run.py build "$(BUILDCFG)" $(YEARS) $(CATEGORIES) $(NAVCNT) $(INDEXCNT)
@mkdocs build -f "$(BUILDCFG)"


Expand Down
79 changes: 56 additions & 23 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def to_dir_name(name:str):

class Post:
# yyyy-mm-dd-post_title.md
NAME_PATTERN = re.compile(r'(?P<YEAR>\d{4})-(?P<MONTH>\d{2})-(?P<DAY>\d{2})-(?P<TITLE>.*).md')
NAME_PATTERN = re.compile(r'(?P<YEAR>\d{4})-(?P<MONTH>\d{2})-(?P<DAY>\d{2})-(?P<TITLE>.*).md')

HLINE = '---'

def __init__(self, post_path:str) -> None:
''' post structure:
Expand Down Expand Up @@ -47,9 +49,9 @@ def to_meta_page(self, dir_name:str):

# meta area
if self.meta:
lines.append('---')
lines.append(Post.HLINE)
lines.extend(self.meta)
lines.append('---')
lines.append(Post.HLINE)

# title area: always has a title
lines.append('\n')
Expand All @@ -60,6 +62,8 @@ def to_meta_page(self, dir_name:str):
links = [f'[{c}]({dir_name}/{to_dir_name(c)}.md)' for c in self.categories]
meta += ' | ' + ' , '.join(links)
lines.append(meta)
lines.append('\n')
lines.append(Post.HLINE)

# content
if self.content:
Expand Down Expand Up @@ -90,13 +94,13 @@ def _process_content(self, src:str):
# potential meta
self.meta = []
next_i = 0
if lines[0].startswith('---'):
if lines[0].startswith(Post.HLINE):
for i, line in enumerate(lines[1:], start=1) :
s = line.strip()
if s == '': continue
if ':' in s:
self.meta.append(s)
elif s.startswith('---'): # normal end of meta
elif s.startswith(Post.HLINE): # normal end of meta
next_i = i+1
break
else: # not valid meta
Expand Down Expand Up @@ -137,7 +141,7 @@ def _extract_content(self, lines):
if s == '':
continue
else:
start = i+1 if s.startswith('---') else i
start = i+1 if s.startswith(Post.HLINE) else i
break
self.content = lines[start:] if start else None

Expand Down Expand Up @@ -184,16 +188,17 @@ def summay_years(self, dir_name:str):
self._to_summary_page(year, page_dir)


def to_index(self, dir_name:str, count:int=5):
def to_index(self, dir_name:str, title:str='', count:int=5):
'''Create home page with latest posts and categories.'''
lines = []
lines = [title]

# latest posts
lines.append('## 最近更新\n')
for post in self._get_latest(count):
lines.append(post.to_hyperlink('.'))

# categories
lines.append('\n')
lines.append('## 更多分类\n')
for c in sorted(self._categories):
lines.append(f'- [{c} ({len(self._posts.get(c))})]({dir_name}/{to_dir_name(c)}.md)')
Expand All @@ -202,7 +207,7 @@ def to_index(self, dir_name:str, count:int=5):
f.write('\n'.join(lines))


def to_navigation(self, dir_name:str, count:int=5):
def to_navigation(self, dir_name:str, count:int=5) -> str:
'''Generate grouped navigation.
::
Expand Down Expand Up @@ -274,39 +279,67 @@ def _to_summary_page(self, category:str, page_dir:str):



def run(docs_dir:str,
class ConfigFile:
def __init__(self, file_path) -> None:
self.file_path = file_path
# read content
with open(file_path, 'r', encoding='utf-8') as f:
self.content = f.read()

def get_site_info(self):
pattern = re.compile(r'site_name:(?P<site>.*)\n(.*)site_description:(?P<desp>.*)\n')
match = pattern.search(self.content)
if match:
site = match.group('site').strip()
description = match.group('desp').strip()
else:
site = 'My Blog'
description = 'Welcome to my blog'
return f'# {site}\n\n{description}\n\n---\n\n'


def update(self, more_content):
with open(self.file_path, 'w', encoding='utf-8') as f:
f.write(self.content + more_content)



def run(cfg_file_path:str,
year_path_name:str='years',
category_path_name:str='categories',
nav_count:int=5,
latest_count:int=5,
update_page:bool=False):

# collect all posts
build_dir = os.path.dirname(cfg_file_path)
docs_dir = os.path.join(build_dir, 'docs')
posts = Posts(docs_dir)

# summary pages by category and year
posts.summay_categories(category_path_name)
posts.summay_years(year_path_name)

# update config file
cfg = ConfigFile(cfg_file_path)
cfg.update(posts.to_navigation(year_path_name, nav_count))

# create index page only if not exist
if not os.path.exists(os.path.join(docs_dir, 'index.md')):
posts.to_index(category_path_name, latest_count)
title = cfg.get_site_info()
posts.to_index(category_path_name, title, latest_count)

# include meta-data to page
if update_page: posts.to_meta_pages(category_path_name)
if update_page:
posts.to_meta_pages(category_path_name)


# navigation
nav_text = posts.to_navigation(year_path_name, nav_count)

return nav_text



if __name__=='__main__':
import sys
# sys.stdout.reconfigure(encoding='utf-8') # python>=3.7
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
import sys

command, docs_dir, years_name, categories_name, nav_count, latest_count = sys.argv[1:]
command, cfg_file, years_name, categories_name, nav_count, latest_count = sys.argv[1:]
nav_count = int(nav_count)
latest_count = int(latest_count)
nav_text = run(docs_dir, years_name, categories_name, nav_count, latest_count, command=='build')
print(nav_text)
run(cfg_file, years_name, categories_name, nav_count, latest_count, command=='build')

0 comments on commit 3a7ad7e

Please sign in to comment.