@ -3,12 +3,13 @@
import collections
import sys
import tempfile
CHAPTER_MARKER = ' ## '
STATUS_MARKER = ' [status]: # '
ACT_MARKER = ' [act]: # '
COMMENT_MARKER = ' [//]: # ' # Strandard markdown comment marker, supported by pandoc and calibre's ebook-convert
# Standard markdown comment marker, supported by Pandoc and Calibre's ebook-convert.
COMMENT_MARKER = ' [//]: # '
def count_words ( line ) :
@ -31,7 +32,8 @@ def main():
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 , tempfile
import MarkdownPP
mdfile = tempfile . TemporaryFile ( mode = ' w+ ' )
MarkdownPP . MarkdownPP ( input = open ( filename ) , output = mdfile , modules = list ( MarkdownPP . modules ) )
mdfile . seek ( 0 )
@ -52,22 +54,24 @@ def main():
word_count_by_act [ act_heading ] + = word_count_by_chapter [ chapter_heading ]
total_word_count + = word_count_by_chapter [ chapter_heading ]
chapter_heading = line [ len ( CHAPTER_MARKER ) : ] . strip ( ' () \n ' )
chapter_heading = line [ len ( CHAPTER_MARKER ) : ] . strip ( ' () \n ' )
word_count_by_chapter [ chapter_heading ] = count_words ( chapter_heading ) # Count the words in chapter heading, because the chapter number and title count as words.
# Count the words in chapter heading, because the chapter number and title count as words.
word_count_by_chapter [ chapter_heading ] = count_words ( chapter_heading )
status_by_chapter [ chapter_heading ] = collections . defaultdict ( int )
current_status = None
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 ' )
# Modified to allow multiple statuses in a single chapter, can swap back and forth.
elif line . startswith ( STATUS_MARKER ) :
if current_status is None :
current_status = line [ len ( STATUS_MARKER ) : ] . strip ( ' () \n ' )
status_by_chapter [ chapter_heading ] [ current_status ] = count_words ( chapter_heading )
else :
current_status = line [ len ( STATUS_MARKER ) : ] . strip ( ' () \n ' )
current_status = line [ len ( STATUS_MARKER ) : ] . strip ( ' () \n ' )
elif line . startswith ( ACT_MARKER ) :
act_heading = line [ len ( ACT_MARKER ) : ] . strip ( ' () \n ' )
act_heading = line [ len ( ACT_MARKER ) : ] . strip ( ' () \n ' )
word_count_by_act [ act_heading ] = count_words ( act_heading )
elif line . startswith ( COMMENT_MARKER ) : # don't count the words in a comment
elif line . startswith ( COMMENT_MARKER ) : # Don't count the words in a comment.
pass
else :
line_word_count = count_words ( line )
@ -83,7 +87,8 @@ 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 : # -c or --chapter to give a chapter-by-chapter word count summary
# -c or --chapter to give a chapter-by-chapter word count summary.
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
@ -102,17 +107,24 @@ def main():
print ( )
if ' -a ' in arguments or ' --act ' in arguments : # -a or --act to give an act-by-act word count summary
# -a or --act to give an act-by-act word count summary.
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 ( ' act {} : {:,} words (~ {} % ) ' . format ( act_heading , act_word_count , act_word_count * 100 / / total_word_count ) )
print (
' act {} : {:,} words (~ {} % ) ' . format (
act_heading , act_word_count , act_word_count * 100 / / total_word_count
)
)
print ( )
for status , status_word_count in word_count_by_status . items ( ) :
print ( f ' { status } : { status_word_count : , } words (~ { status_word_count * 100 / / total_word_count } %) ' )
print (
f ' { status } : { status_word_count : , } words (~ { status_word_count * 100 / / total_word_count } %) '
)
print ( f ' total: { total_word_count : , } words ' )