Commented my changes + swapped to tempfile library
This commit is contained in:
parent
d79e3f5b80
commit
e3a47d7425
@ -3,13 +3,12 @@
|
||||
|
||||
import collections
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
CHAPTER_MARKER = '## '
|
||||
STATUS_MARKER = '[status]: # '
|
||||
ACT_MARKER = '[act]: # '
|
||||
COMMENT_MARKER = '[//]: # '
|
||||
COMMENT_MARKER = '[//]: # ' # Strandard markdown comment marker, supported by pandoc and calibre's ebook-convert
|
||||
|
||||
|
||||
def count_words(line):
|
||||
@ -27,13 +26,17 @@ def count_words(line):
|
||||
def main():
|
||||
arguments = sys.argv[1:]
|
||||
filename = arguments[0]
|
||||
tmpfilename = None
|
||||
mdfile = None
|
||||
|
||||
if '-pp' in arguments:
|
||||
import MarkdownPP
|
||||
tmpfilename = f'.tmp-{os.getpid}.md'
|
||||
MarkdownPP.MarkdownPP(input=open(filename), output=open(tmpfilename,'w'), modules=list(MarkdownPP.modules))
|
||||
filename = tmpfilename
|
||||
# -pp flag to allow Markdown Preprocessing primarily to allow multi-file novel formatting
|
||||
# this is implemented using a temporary file created using python's buit-in tempfile library
|
||||
import MarkdownPP, tempfile
|
||||
mdfile = tempfile.TemporaryFile(mode='w+')
|
||||
MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules))
|
||||
mdfile.seek(0)
|
||||
else:
|
||||
mdfile = open(filename)
|
||||
|
||||
chapter_heading = None
|
||||
act_heading = None
|
||||
@ -44,7 +47,7 @@ def main():
|
||||
status_by_chapter = {}
|
||||
current_status = None
|
||||
|
||||
for line in open(filename).readlines():
|
||||
for line in mdfile.readlines():
|
||||
if line.startswith(CHAPTER_MARKER):
|
||||
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||
total_word_count += word_count_by_chapter[chapter_heading]
|
||||
@ -55,7 +58,7 @@ def main():
|
||||
|
||||
status_by_chapter[chapter_heading] = collections.defaultdict(int)
|
||||
current_status = None
|
||||
elif line.startswith(STATUS_MARKER):
|
||||
elif line.startswith(STATUS_MARKER): # Modified to allow multiple statuses in a single chapter, can swap back and forth.
|
||||
if current_status == None:
|
||||
current_status = line[len(STATUS_MARKER):].strip('()\n')
|
||||
status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading)
|
||||
@ -65,7 +68,7 @@ def main():
|
||||
elif line.startswith(ACT_MARKER):
|
||||
act_heading = line[len(ACT_MARKER):].strip('()\n')
|
||||
word_count_by_act[act_heading] = count_words(act_heading)
|
||||
elif line.startswith(COMMENT_MARKER):
|
||||
elif line.startswith(COMMENT_MARKER): # don't count the words in a comment
|
||||
pass
|
||||
else:
|
||||
line_word_count = count_words(line)
|
||||
@ -79,8 +82,7 @@ def main():
|
||||
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||
total_word_count += word_count_by_chapter[chapter_heading]
|
||||
|
||||
if '-c' in arguments or '--chapter' in arguments:
|
||||
# Print out word counts.
|
||||
if '-c' in arguments or '--chapter' in arguments: # -c or --chapter to give a chapter-by-chapter word count summary
|
||||
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
||||
if chapter_heading is None:
|
||||
continue
|
||||
@ -99,7 +101,7 @@ def main():
|
||||
|
||||
print()
|
||||
|
||||
if '-a' in arguments or '--act' in arguments:
|
||||
if '-a' in arguments or '--act' in arguments: # -a or --act to give an act-by-act word count summary
|
||||
for act_heading, act_word_count in word_count_by_act.items():
|
||||
if act_heading is None:
|
||||
continue
|
||||
@ -109,12 +111,11 @@ def main():
|
||||
print()
|
||||
|
||||
for status, status_word_count in word_count_by_status.items():
|
||||
print('{}: {:,} words (~{}%)'.format(status, status_word_count, status_word_count * 100 // total_word_count))
|
||||
print(f'{status}: {status_word_count:,} words (~{status_word_count * 100 // total_word_count}%)')
|
||||
|
||||
print('total: {:,} words'.format(total_word_count))
|
||||
print(f'total: {total_word_count:,} words')
|
||||
|
||||
if tmpfilename:
|
||||
os.remove(tmpfilename)
|
||||
mdfile.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user