Add support for chapter ranges.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
ae29f1e2a2
commit
8460879f73
|
@ -6,17 +6,32 @@ import sys
|
||||||
import docx
|
import docx
|
||||||
|
|
||||||
|
|
||||||
def filter_chapters(document, chapter_numbers):
|
def expand_chapter_range(chapter_request):
|
||||||
if not chapter_numbers:
|
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
|
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:
|
for paragraph in document.paragraphs:
|
||||||
if paragraph.style.name == 'Heading 2':
|
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 = paragraph._element
|
||||||
element.getparent().remove(element)
|
element.getparent().remove(element)
|
||||||
|
|
||||||
|
@ -109,8 +124,7 @@ def parse_arguments(unparsed_arguments):
|
||||||
'--chapters',
|
'--chapters',
|
||||||
metavar='CHAPTER',
|
metavar='CHAPTER',
|
||||||
nargs='+',
|
nargs='+',
|
||||||
type=int,
|
help='Chapter numbers or ranges to include in output',
|
||||||
help='Numbers of chapters to include in output',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return parser.parse_args(unparsed_arguments)
|
return parser.parse_args(unparsed_arguments)
|
||||||
|
|
Loading…
Reference in New Issue