分成兩個檔案。 easyWebBook.py tools_and_settings.py funcs_and_constants.py funcs_and_constants.py 最前面的 autorename = False 改成 True 之後,會自動依 #chapter| 後面的 tag 決定並自動更改檔名。 easyWebBook.py
#!/usr/python3
# encoding:utf8
from copy import deepcopy
from operator import attrgetter
import sys
import fucns_and_constants as ts
class Path:
def __init__(self):
self.fs: list = []
self.months: list = []
self.py: str = __file__.rsplit('/', maxsplit=1)[0]
self.source: str = ts.filesource
self.html: str = self.source.replace('/Books/_source', '/Books/html')
img: str = ts.join_path(self.html, 'img')
css: str = ts.join_path(ts.splits_last(self.source, '/', 2)[0],
'_share/styles.css')
ts.create_dir(img)
ts.shell(f'cp {css} {img}/')
self.bookfile: str = self.locate_file_by_end('.book')[0]
def locate_file_by_end(self, end: str) -> list:
return [ts.join_path(self.source, _)
for _ in ts.filelist(self.source)
if _.endswith(end)]
def locate_file_by_start(self, start: str) -> list:
return [ts.join_path(self.source, _)
for _ in ts.filelist(self.source)
if _.startswith(start)]
def locate_file_by_both(self, start: str, end: str) -> list:
return [ts.join_path(self.source, _)
for _ in ts.filelist(self.source)
if _.startswith(start) and _.endswith(end)]
paths = Path()
class File:
def __init__(self, f: str):
self.src: str = ts.join_path(paths.source, f)
f = f[:-4]
self.dst: str = ts.concat(paths.html, '/', f, '.html')
self.name, self.month, self.id = f.split('_')
self.mon: str = self.month[-2:]
class MChapter:
def __init__(self, fs: list, m: str, c: str):
self.name: str = c
self.month: str = m
self.fs: list = [_ for _ in fs if _.name == c and _.month == m]
self.fs.sort(key=attrgetter('id'), reverse=True)
# self.fs = ts.group_by_amount(self.fs, 7)
if not self.fs:
del self
class Month:
def __init__(self, chap: list, m: str) -> None:
self.fs: list = []
self.month: str = m
self.chapters: list = [_ for _ in chap if _.month == m]
def flatten_to_fs(self) -> list:
self.fs = [_.fs for _ in self.chapters]
return list(sum(self.fs, []))
class Book:
def __init__(self):
self.ct: list = []
"""#### Book Content: chpater files (object FILE)"""
self._chapters: list = []
"""#### list of chapter names """
self._topics: dict = {}
"""#### dict of anchors"""
self.book: str = ''
"""#### book name"""
self.read_book_file()
self.prepare_files()
def read_book_file(self) -> None:
for _ in ts.read(paths.bookfile).splitlines():
if _.startswith(' '):
self._chapters.append(_.strip().replace(' ', ''))
else:
self.book = _.strip(' ')
def prepare_files(self) -> None:
fs = [File(_) for _ in ts.filelist(paths.source) if _.endswith('.txt')]
months = sorted(list(set([_.month for _ in fs])), reverse=True)
chapt = [MChapter(fs, m, c) for c in self._chapters for m in months]
chapt = [_ for _ in chapt if _.fs]
self.ct = [Month(chapt, m) for m in months]
def flatten(self) -> None:
self.ct = [_.flatten_to_fs() for _ in self.ct]
self.ct = list(sum(self.ct, []))
def reg_topics(self, k: str, v: str, f: str) -> None:
url = ts.link.format(url=ts.concat('file:///', f.dst),
sect='_'.join([k, v]),
tex=v)
url = url.replace('////', '///')
if k not in self._topics:
self._topics[k] = [url]
else:
self._topics[k].append(url)
book = Book()
def make_home_file():
html = ts.concat(ts.h.format(n=1, tex=book.book),
ts.newline)
item = '<a href="file:///{url}">{tex}</a>    '
for m in book.ct:
html += f'<h2 style="color:#f3cf80;" id="{m.month}">{m.month}</h2>'
for c in m.chapters:
html += ts.h.format(n=3, tex=c.name)
if len(c.fs) >= 7:
ts.group_by_amount(c.fs, 7)
for g in c.fs:
for f in g:
html += item.format(url=f.dst, tex=f.id).replace('////', '///')
html += ts.newline
html += ts.newline
else:
for f in c.fs:
html += item.format(url=f.dst, tex=f.id).replace('////', '///')
html += ts.newline
html += ts.newline
html += '''<h2 style="color:#f3cf80" id="and">And</h2>
<h3><a href="topics.html">Topics</a></h3>
<br>
'''
html = ts.frame.format(body=html)
ts.save(ts.join_path(paths.html, 'Home.html'),
html)
def make_page_file():
parser = ts.TagsParser()
parser.book = book
for f in book.ct:
raw = ts.read(f.src).strip()
for symbols in ts.PosReplacing:
raw = ts.pos_replacing(raw, *symbols)
chapter, raw = ts.split_first(raw, '\n')
chapter = chapter.split('|')[-3:]
chapter_tex = '   '.join(chapter)
# 用檔案第一行 chapter tags 來決定檔名,並自動更改實際檔名
fname = ts.concat('_'.join(chapter), '.txt')
filename = ts.join_path(ts.splits_last(f.src, '/', 1)[0], fname)
if ts.autorename and f.src != filename:
ts.os.rename(f.src, filename)
f.dst = ts.concat(ts.splits_last(f.dst, '/', 1)[0], '/', fname, ',html')
# region [ rgba(255, 0, 255, 0.1) ] ━━━━━━━━━━━━ PAGE HEAD
if raw.startswith('#log'):
log, raw = ts.split_first(raw, '#endlog')
log = ts.newline.join(log.splitlines()[1:])
header = parser.head.format(tex=chapter_tex, log=log, date=chapter[1])
else:
header = parser.head.format(tex=chapter_tex, log='', date=chapter[1])
# endregion ─────────────────────────────
raw = ts.splits_last(raw.strip(), '#endchapter', 1)[0].strip()
# region [ rgba(0, 255, 255, 0.03) ] ━━━━━━━━━━ Line by Line
parser.setup(chapter[1], f)
raw = raw.splitlines()
for idx, line in enumerate(raw):
if not parser.opened and not line.startswith('#'):
line = line.replace(' ', parser.nbsp).replace(' ', parser.emsp)
continue
if parser.opened and not line.startswith('#'):
line = line.replace(' ', parser.nbsp).replace(' ', parser.emsp)
raw[idx] = parser.method(line)
continue
raw[idx] = parser.parse_tag(line)
raw = '<br>'.join([_ for _ in raw if _ != '#del|'])
# endregion ─────────────────────────────
# region [ rgba(255, 255, 0, 0.06) ] ━━━━━━━━━━━━━ Index
index = parser.index
if len(index) > 7:
index = ts.group_by_amount(index, 8)
index = str(ts.newline + ts.newline).join([ts.emsps.join(_) for _ in index])
else:
index = ts.emsps.join(index)
index += ts.newline
# endregion ─────────────────────────────
html = ts.frame.format(body=header + index + raw)
ts.save(f.dst, html)
def make_topic():
keys = sorted(list(book._topics.keys()))
html = ''
for k in keys:
html += f'<h2 id="{k}">{k}</h2>\n'
v = sorted(book._topics[k])
# v = [[_, calc_length(_)] for _ in v]
if len(v) == 1:
html += v[0] + ts.newline
continue
tex = [_.split('>')[1].split('<')[0] for _ in v]
ref = [''.join(['1' for _ in x]) for x in tex]
ref = str('0' * len(ts.emsps)).join(ref)
while ref:
r = ref[:84]
if r.endswith('1'):
r = list(filter(None, r.split('0')[:-1]))
else:
r = list(filter(None, r.split('0')))
tex = v[:len(r)]
html += ts.emsps.join(tex) + ts.newline
ref = ref[84:]
html += ts.newline
html = ts.frame.format(body=html)
ts.save(ts.join_path(paths.html, 'topics.html'),
html)
if __name__ == "__main__":
make_home_file()
book.flatten()
make_page_file()
make_topic()
Comments