Compare commits

..

No commits in common. "5df3a48f35ba39d0a8f4f740e6b8eb457be7bd04" and "aa9511c4cb2ddd1d3f424310ee26f6b3401176a8" have entirely different histories.

View File

@ -1,8 +1,8 @@
#!/usr/bin/python
import argparse
import collections
import sys
import tempfile
CHAPTER_MARKER = '## '
@ -25,46 +25,20 @@ def count_words(line):
def main():
# Better argument parsing
parser = argparse.ArgumentParser()
parser.add_argument(
'-c',
'--chapter',
action='store_true',
help='output chapter-by-chapter breakdown of word counts, including how many words in each chapter are tagged with which status',
)
parser.add_argument(
'-a',
'--act',
action='store_true',
help='output act-by-act breakdown of word counts (total only)',
)
parser.add_argument(
'-pp',
action='store_true',
help='run markdown pre-processor, this allows for a multi-file input (e.g. each chapter in its own file), but requires the MarkdownPP python library',
)
parser.add_argument(
'markdown_file',
type=argparse.FileType('r'),
help='The markdown file for the novel, main file if a multi-file novel',
)
arguments = parser.parse_args()
arguments = sys.argv[1:]
filename = arguments[0]
mdfile = None
if arguments.pp:
if '-pp' in arguments:
# -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
mdfile = tempfile.TemporaryFile(mode='w+')
MarkdownPP.MarkdownPP(
input=arguments.markdown_file, output=mdfile, modules=list(MarkdownPP.modules)
)
MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules))
mdfile.seek(0)
else:
mdfile = arguments.markdown_file
mdfile = open(filename)
chapter_heading = None
act_heading = None
@ -114,7 +88,7 @@ def main():
total_word_count += word_count_by_chapter[chapter_heading]
# -c or --chapter to give a chapter-by-chapter word count summary.
if arguments.chapter:
if '-c' in arguments or '--chapter' in arguments:
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
if chapter_heading is None:
continue
@ -134,13 +108,15 @@ def main():
print()
# -a or --act to give an act-by-act word count summary.
if arguments.act:
if '-a' in arguments or '--act' in arguments:
for act_heading, act_word_count in word_count_by_act.items():
if act_heading is None:
continue
print(
f'act {act_heading}: {act_word_count:,} words (~{act_word_count * 100// total_word_count}%)'
'act {}: {:,} words (~{}%)'.format(
act_heading, act_word_count, act_word_count * 100 // total_word_count
)
)
print()