Add support for chapter ranges.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Dan Helfman 2023-11-04 21:39:49 -07:00
parent ae29f1e2a2
commit 8460879f73
1 changed files with 21 additions and 7 deletions

View File

@ -6,17 +6,32 @@ import sys
import docx
def filter_chapters(document, chapter_numbers):
if not chapter_numbers:
def expand_chapter_range(chapter_request):
if not '-' in chapter_request:
return (int(chapter_request),)
chapter_range = chapter_request.split('-')
return tuple(range(int(chapter_range[0]), int(chapter_range[1]) + 1))
def filter_chapters(document, chapter_requests):
if not chapter_requests:
return
chapter_number = 0
chapter_numbers = {
chapter_number
for request in chapter_requests
for chapter_number in expand_chapter_range(request)
}
current_chapter_number = 0
for paragraph in document.paragraphs:
if paragraph.style.name == 'Heading 2':
chapter_number += 1
current_chapter_number += 1
if chapter_number not in chapter_numbers:
if current_chapter_number not in chapter_numbers:
element = paragraph._element
element.getparent().remove(element)
@ -109,8 +124,7 @@ def parse_arguments(unparsed_arguments):
'--chapters',
metavar='CHAPTER',
nargs='+',
type=int,
help='Numbers of chapters to include in output',
help='Chapter numbers or ranges to include in output',
)
return parser.parse_args(unparsed_arguments)