Skip to content

Commit

Permalink
v0.3.0+build.1784
Browse files Browse the repository at this point in the history
- 5.17,扩充了“久而敬之”的解释,增加了参考引文。
- 7.24,增加了本章意义的阐述及《论语新解》引文。
- 9.31,充实了“唐棣之华”4句诗的解释,增加了《诗经》的例证。
- 11.24,“备员”改用《史记》例。
- 16.1,增加了“固”、“危而不持,颠而不扶”的解释。
- 17.16,改进了“廉”的解释和例证。
- 18.2,增加了“父母之邦”的解释。
- 充实了柳下惠的小传。
- 补充了诸弟子墓葬处,以便追思。
- 略修改了主题索引的引言;为“孔子自评”、“人评孔子”增加了链接。
- 加大了blob间距(after skip),视觉效果更好一些。
- Python脚本改用Linux换行方式。
  • Loading branch information
abolybook committed Apr 20, 2017
1 parent ac209db commit f03a32a
Show file tree
Hide file tree
Showing 15 changed files with 938 additions and 914 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ Web site (Chinese): http://www.abolybook.org
编译用到了:

- xelatex,调用的包和字体,见 style.tex
- Python,2.7和3.5皆可,调用了 [pinyin](https://pypi.python.org/pypi/pinyin/)
- Python,2.7和3皆可,调用了 [pinyin](https://pypi.python.org/pypi/pinyin/)

网站:http://www.abolybook.org
88 changes: 44 additions & 44 deletions autobody.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
# coding: utf-8

from __future__ import unicode_literals
import io
import sys


# \lychar* --> \lycharlink
def auto_lychars(bodyfile, cmapfile, encoding):
with io.open(bodyfile, encoding=encoding) as fin:
body = fin.read()
with io.open(cmapfile, encoding=encoding) as fin:
cmaps = fin.read().splitlines()
for cm in cmaps:
chunks = cm.split(' ')
target = chunks[-1]
for name in chunks[:-1]:
old = r'\lychar{%s}' % name
repl = r'\lycharlink{%s}{%s}' % (target, name)
body = body.replace(old, repl)

specials = {
r'\lychar{伯夷}': r'\lycharlink{boyishuqi}{伯夷}',
r'\lychar{叔齐}': r'\lycharlink{boyishuqi}{叔齐}',
}
for old, repl in specials.items():
body = body.replace(old, repl)
return body


def main():
CHARACTERS_MAP = 'characters_map.txt'
ENCODING = 'utf-8'
BODY = 'body.tex'
BODY_OUT = 'autobody.tex'

body = auto_lychars(BODY, CHARACTERS_MAP, ENCODING)

with io.open(BODY_OUT, 'w', encoding=ENCODING) as fout:
fout.write(body)


if __name__ == '__main__':
sys.exit(main())
# coding: utf-8

from __future__ import unicode_literals
import io
import sys


# \lychar* --> \lycharlink
def auto_lychars(bodyfile, cmapfile, encoding):
with io.open(bodyfile, encoding=encoding) as fin:
body = fin.read()
with io.open(cmapfile, encoding=encoding) as fin:
cmaps = fin.read().splitlines()
for cm in cmaps:
chunks = cm.split(' ')
target = chunks[-1]
for name in chunks[:-1]:
old = r'\lychar{%s}' % name
repl = r'\lycharlink{%s}{%s}' % (target, name)
body = body.replace(old, repl)

specials = {
r'\lychar{伯夷}': r'\lycharlink{boyishuqi}{伯夷}',
r'\lychar{叔齐}': r'\lycharlink{boyishuqi}{叔齐}',
}
for old, repl in specials.items():
body = body.replace(old, repl)
return body


def main():
CHARACTERS_MAP = 'characters_map.txt'
ENCODING = 'utf-8'
BODY = 'body.tex'
BODY_OUT = 'autobody.tex'

body = auto_lychars(BODY, CHARACTERS_MAP, ENCODING)

with io.open(BODY_OUT, 'w', encoding=ENCODING) as fout:
fout.write(body)


if __name__ == '__main__':
sys.exit(main())
244 changes: 122 additions & 122 deletions autocharacters.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
# coding: utf-8

from __future__ import unicode_literals
import io
import re
import sys


def extract_blob_title(content):
r'''Extract text inside \lyblob{}.'''
left = content.index('{') + 1
right = left
count = 1
while count != 0:
if content[right] == '{':
count += 1
elif content[right] == '}':
count -= 1
right += 1
return content[left:right]


def get_charname_blobs(content):
'''Return a dict of character names to lists of blob labels.'''
CHAPTER_PREFIX = r'\chapter'
BLOB_PREFIX = r'\lyblob'
BLOB_TEMPLATE = '%d.%d'
CHARNAME_PAT = re.compile(r'\\lycharlink\{(.+?)\}\{.+?\}')
chapter_count = 0
blob_count = 0

lines = content.splitlines(True)
c2bs = {}
for i, line in enumerate(lines):
if line.lstrip().startswith(CHAPTER_PREFIX):
chapter_count += 1
blob_count = 0
elif line.lstrip().startswith(BLOB_PREFIX):
blob_count += 1
left = content.index(line)
assert content[left+len(line):].find(line) == -1 # no duplicate \lyblob line
title = extract_blob_title(content[left:])
mats = CHARNAME_PAT.finditer(title)
if mats:
blob = BLOB_TEMPLATE % (chapter_count, blob_count)
# if blob == BLOB_TEMPLATE % (11, 3): # skip 四科十哲
# continue
for m in mats:
charname = m.group(1)
if charname not in c2bs:
c2bs[charname] = [blob]
else:
if blob not in c2bs[charname]:
c2bs[charname].append(blob)
return c2bs


def append_annotation(seg, annotation):
insert_pos = len(seg)
while seg[insert_pos-1].isspace():
insert_pos -= 1
return seg[:insert_pos] + annotation + seg[insert_pos:]


def append_annotations(content, charname_blobs):
removecomment_pat = re.compile(r'(?<!\\)%.+', re.M)
content = removecomment_pat.sub('', content)

charlabel_pat = re.compile(r'(?:^\\lypdfbookmark)|(?:^\\lylabel\{(\w+)\})', re.M)
skip_labels = set(('zisi', 'shaogong', 'boyi', 'lijiliyun'))
segs = []
pos = 0
label, copy = '', True
for mat in charlabel_pat.finditer(content):
start = mat.start()
seg = content[pos: start]
pos = start
if copy:
segs.append(seg)
else:
blobs = charname_blobs[label]
annotation = ' ' + ' '.join(r'\lyref{%s}' % b for b in blobs)
segs.append(append_annotation(seg, annotation))
if mat.group() == r'\lypdfbookmark':
copy = True
continue
label = mat.group(1)
if label in skip_labels:
copy = True
skip_labels.remove(label)
else:
copy = False
seg = content[pos:]
if copy:
segs.append(seg)
else:
blobs = charname_blobs[label]
annotation = ' ' + ' '.join(r'\lyref{%s}' % b for b in blobs)
segs.append(append_annotation(seg, annotation))
assert not skip_labels
return ''.join(segs)


def main():
AUTOBODY = 'autobody.tex' # parse \lycharlink{tag}{name} in \lyblob's
CHARACTERS = 'characters.tex'
CHARACTERS_OUT = 'autocharacters.tex'
ENCODING = 'utf-8'
with io.open(AUTOBODY, encoding=ENCODING) as fin:
body = fin.read()
with io.open(CHARACTERS, encoding=ENCODING) as fin:
characters_content = fin.read()

charname_blobs = get_charname_blobs(body)
auto_characters_content = append_annotations(characters_content, charname_blobs)

with io.open(CHARACTERS_OUT, 'w', encoding=ENCODING) as fout:
fout.write(auto_characters_content)


if __name__ == '__main__':
sys.exit(main())
# coding: utf-8

from __future__ import unicode_literals
import io
import re
import sys


def extract_blob_title(content):
r'''Extract text inside \lyblob{}.'''
left = content.index('{') + 1
right = left
count = 1
while count != 0:
if content[right] == '{':
count += 1
elif content[right] == '}':
count -= 1
right += 1
return content[left:right]


def get_charname_blobs(content):
'''Return a dict of character names to lists of blob labels.'''
CHAPTER_PREFIX = r'\chapter'
BLOB_PREFIX = r'\lyblob'
BLOB_TEMPLATE = '%d.%d'
CHARNAME_PAT = re.compile(r'\\lycharlink\{(.+?)\}\{.+?\}')
chapter_count = 0
blob_count = 0

lines = content.splitlines(True)
c2bs = {}
for i, line in enumerate(lines):
if line.lstrip().startswith(CHAPTER_PREFIX):
chapter_count += 1
blob_count = 0
elif line.lstrip().startswith(BLOB_PREFIX):
blob_count += 1
left = content.index(line)
assert content[left+len(line):].find(line) == -1 # no duplicate \lyblob line
title = extract_blob_title(content[left:])
mats = CHARNAME_PAT.finditer(title)
if mats:
blob = BLOB_TEMPLATE % (chapter_count, blob_count)
# if blob == BLOB_TEMPLATE % (11, 3): # skip 四科十哲
# continue
for m in mats:
charname = m.group(1)
if charname not in c2bs:
c2bs[charname] = [blob]
else:
if blob not in c2bs[charname]:
c2bs[charname].append(blob)
return c2bs


def append_annotation(seg, annotation):
insert_pos = len(seg)
while seg[insert_pos-1].isspace():
insert_pos -= 1
return seg[:insert_pos] + annotation + seg[insert_pos:]


def append_annotations(content, charname_blobs):
removecomment_pat = re.compile(r'(?<!\\)%.+', re.M)
content = removecomment_pat.sub('', content)

charlabel_pat = re.compile(r'(?:^\\lypdfbookmark)|(?:^\\lylabel\{(\w+)\})', re.M)
skip_labels = set(('zisi', 'shaogong', 'boyi', 'lijiliyun'))
segs = []
pos = 0
label, copy = '', True
for mat in charlabel_pat.finditer(content):
start = mat.start()
seg = content[pos: start]
pos = start
if copy:
segs.append(seg)
else:
blobs = charname_blobs[label]
annotation = ' ' + ' '.join(r'\lyref{%s}' % b for b in blobs)
segs.append(append_annotation(seg, annotation))
if mat.group() == r'\lypdfbookmark':
copy = True
continue
label = mat.group(1)
if label in skip_labels:
copy = True
skip_labels.remove(label)
else:
copy = False
seg = content[pos:]
if copy:
segs.append(seg)
else:
blobs = charname_blobs[label]
annotation = ' ' + ' '.join(r'\lyref{%s}' % b for b in blobs)
segs.append(append_annotation(seg, annotation))
assert not skip_labels
return ''.join(segs)


def main():
AUTOBODY = 'autobody.tex' # parse \lycharlink{tag}{name} in \lyblob's
CHARACTERS = 'characters.tex'
CHARACTERS_OUT = 'autocharacters.tex'
ENCODING = 'utf-8'
with io.open(AUTOBODY, encoding=ENCODING) as fin:
body = fin.read()
with io.open(CHARACTERS, encoding=ENCODING) as fin:
characters_content = fin.read()

charname_blobs = get_charname_blobs(body)
auto_characters_content = append_annotations(characters_content, charname_blobs)

with io.open(CHARACTERS_OUT, 'w', encoding=ENCODING) as fout:
fout.write(auto_characters_content)


if __name__ == '__main__':
sys.exit(main())
Loading

0 comments on commit f03a32a

Please sign in to comment.