diff --git a/README.md b/README.md index c6bf7f8..4afcab7 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,20 @@ Example usage: $ format-novel manuscript.docx ``` -This assumes the document already contains a single title page. It applies the -following formatting, modifying the file in-place: +This assumes the document already contains a single title page, which is +skipped for formatting purposes. It applies the following formatting, +modifying the file in-place: * apply double spacing * indent the first line of each paragraph (0.5 inches) except for the initial paragraph in a chapter or section * remove vertical spacing between paragraphs * set a default font of Times New Roman - * enable page numbering starting on the second page (upper right) or the first page if filtering by chapter + * put the author last name, project title, and page number into the page header + (e.g. "Corey / Leviathan Wakes / 99") -### Chapter excerpting +### Chapter excerpts You can optionally filter the output to only include certain chapters by number. For instance: @@ -35,10 +37,16 @@ other chapters. For this to work, the input document must indicate chapter titles with the "Heading 2" style. -## Planned features +## Page header - * Support for specifying author and project title to show up in the header - alongside page number. +When constructing the page header, format-novel tries to determine the author +last name from the first line of the manuscript and the project title from the +filename. But if it gets those wrong, you can specify them on them on the +command-line. For example: + +```bash +$ format-novel manuscript.docx --author Corey --title "Leviathan Wakes" +``` ## Installation diff --git a/format_novel/format_novel.py b/format_novel/format_novel.py index fb1f19f..5cbbbdf 100755 --- a/format_novel/format_novel.py +++ b/format_novel/format_novel.py @@ -1,6 +1,7 @@ #!/usr/bin/python import argparse +import os import sys import docx @@ -136,8 +137,20 @@ def parse_arguments(unparsed_arguments): '--chapters', metavar='CHAPTER', nargs='+', - help='Chapter numbers or ranges to include in output', + help='Chapter numbers or ranges to include in output, defaults to all', ) + parser.add_argument( + '-a', + '--author', + metavar='LAST NAME', + help='Author last name to show in page headers, defaults to parsing the first line of the manuscript for the name', + ) + parser.add_argument( + '-t', + '--title', + help='Project title to show in page headers, defaults to the manuscript filename without the file extension', + ) + return parser.parse_args(unparsed_arguments) @@ -145,10 +158,12 @@ def parse_arguments(unparsed_arguments): def main(): arguments = parse_arguments(sys.argv[1:]) document = docx.Document(arguments.document_filename) + author_last_name = arguments.author or document.paragraphs[0].text.split('\n')[0].split(' ')[-1] + project_title = arguments.title or os.path.splitext(arguments.document_filename)[0] filter_chapters(document, arguments.chapters) set_default_font(document) - # add_header_text(document, 'Author / Project Title / ') + add_header_text(document, f'{author_last_name} / {project_title} / ') add_header_page_number(document) if not arguments.chapters: