Код: Выделить всё
Contents 0
1. Chapter 1 1
1.1. Subchapter 1.1. 2
1.2 Subchapter 1.2 3
2. Chapter 2 4
2.1 Subchapter 2.1 5
2.1.1 Subsubchapter 6
Index 7
Код: Выделить всё
./text_to_json.py outline.json
Код: Выделить всё
[
{
"title": "Contents",
"dest": 0
},
{
"title": "1. Chapter 1",
"dest": 1,
"children": [
{
"title": "1.1. Subchapter 1.1.",
"dest": 2
},
{
"title": "1.2 Subchapter 1.2",
"dest": 3
}
]
},
{
"title": "2. Chapter 2",
"dest": 4,
"children": [
{
"title": "2.1 Subchapter 2.1",
"dest": 5,
"children": [
{
"title": "2.1.1 Subsubchapter",
"dest": 6
}
]
},
{
"title": "Index",
"dest": 7
}
]
}
]
Код: Выделить всё
if not last_indent.startswith(space):
raise Exception(
f"invalid de-indentation at line {line_number}")
while len(last_indent) != len(space):
# NOTE: We rely on preconditions here to avoid more assertions
# about stack state.
indent_stack.pop()
last_indent = indent_stack[-1]
Код: Выделить всё
while indent_stack and len(indent_stack[-1]) > len(space):
indent_stack.pop()
# Check if the remaining indentation matches exactly
if not indent_stack or indent_stack[-1] != space:
raise Exception(
f"invalid de-indentation at line {line_number}")
Подробнее здесь: https://stackoverflow.com/questions/791 ... conversion