forked from witten/novel-stats
commit
7ee1c7c439
|
@ -21,7 +21,7 @@ total: 539 words
|
||||||
Example output with chapter data:
|
Example output with chapter data:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ novel-stats example.md -c
|
$ novel-stats -c example.md
|
||||||
chapter 1: 103 (drafted)
|
chapter 1: 103 (drafted)
|
||||||
chapter 2: 83 (dev edited)
|
chapter 2: 83 (dev edited)
|
||||||
chapter 3: 115
|
chapter 3: 115
|
||||||
|
@ -36,7 +36,7 @@ total: 539 words
|
||||||
Example with multi-file markdown:
|
Example with multi-file markdown:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ novel-stats multi_file.mdpp -pp -c -a
|
$ novel-stats -pp -c -a multi_file.mdpp
|
||||||
chapter 1 Lorem:
|
chapter 1 Lorem:
|
||||||
203 (drafted)
|
203 (drafted)
|
||||||
303 (dev edited)
|
303 (dev edited)
|
||||||
|
@ -70,7 +70,7 @@ novel-stats takes a single argument: The path to your novel file in markdown
|
||||||
format. For instance:
|
format. For instance:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
novel-stats /path/to/your/novel.md[pp] [-c/--chapter] [-a/--act] [-pp]
|
novel-stats [-c/--chapter] [-a/--act] [-pp] /path/to/your/novel.md[pp]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Optional flags
|
### Optional flags
|
||||||
|
@ -78,7 +78,7 @@ novel-stats /path/to/your/novel.md[pp] [-c/--chapter] [-a/--act] [-pp]
|
||||||
* `-c` or `--chapter` — output chapter-by-chapter breakdown of word counts,
|
* `-c` or `--chapter` — output chapter-by-chapter breakdown of word counts,
|
||||||
including how many words in each chapter are tagged with which status
|
including how many words in each chapter are tagged with which status
|
||||||
* `-a` or `--act` — output act-by-act breakdown of word counts (total only)
|
* `-a` or `--act` — output act-by-act breakdown of word counts (total only)
|
||||||
* `--pp` — run markdown pre-processor, this allows for a multi-file input
|
* `-pp` — run markdown pre-processor, this allows for a multi-file input
|
||||||
(e.g. each chapter in its own file), but requires the MarkdownPP python
|
(e.g. each chapter in its own file), but requires the MarkdownPP python
|
||||||
library.
|
library.
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
|
||||||
|
import argparse
|
||||||
import collections
|
import collections
|
||||||
import sys
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
CHAPTER_MARKER = '## '
|
CHAPTER_MARKER = '## '
|
||||||
|
@ -25,20 +25,46 @@ def count_words(line):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
arguments = sys.argv[1:]
|
# Better argument parsing
|
||||||
filename = arguments[0]
|
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()
|
||||||
|
|
||||||
mdfile = None
|
mdfile = None
|
||||||
|
|
||||||
if '-pp' in arguments:
|
if arguments.pp:
|
||||||
# -pp flag to allow Markdown Preprocessing primarily to allow multi-file novel formatting
|
# -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
|
# this is implemented using a temporary file created using python's buit-in tempfile library
|
||||||
import MarkdownPP
|
import MarkdownPP
|
||||||
|
|
||||||
mdfile = tempfile.TemporaryFile(mode='w+')
|
mdfile = tempfile.TemporaryFile(mode='w+')
|
||||||
MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules))
|
MarkdownPP.MarkdownPP(
|
||||||
|
input=arguments.markdown_file, output=mdfile, modules=list(MarkdownPP.modules)
|
||||||
|
)
|
||||||
mdfile.seek(0)
|
mdfile.seek(0)
|
||||||
else:
|
else:
|
||||||
mdfile = open(filename)
|
mdfile = arguments.markdown_file
|
||||||
|
|
||||||
chapter_heading = None
|
chapter_heading = None
|
||||||
act_heading = None
|
act_heading = None
|
||||||
|
@ -88,7 +114,7 @@ def main():
|
||||||
total_word_count += word_count_by_chapter[chapter_heading]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
|
|
||||||
# -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:
|
if arguments.chapter:
|
||||||
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
||||||
if chapter_heading is None:
|
if chapter_heading is None:
|
||||||
continue
|
continue
|
||||||
|
@ -108,15 +134,13 @@ def main():
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# -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:
|
if arguments.act:
|
||||||
for act_heading, act_word_count in word_count_by_act.items():
|
for act_heading, act_word_count in word_count_by_act.items():
|
||||||
if act_heading is None:
|
if act_heading is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(
|
print(
|
||||||
'act {}: {:,} words (~{}%)'.format(
|
f'act {act_heading}: {act_word_count:,} words (~{act_word_count * 100// total_word_count}%)'
|
||||||
act_heading, act_word_count, act_word_count * 100 // total_word_count
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user