Skip to content

Commit 00f8b6e

Browse files
committed
Merge remote-tracking branch 'upstream/master'.
2 parents d4e96e4 + b5a3034 commit 00f8b6e

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@ $ pip install pdfbookmarker
1313

1414
## Usage
1515

16-
1) Turn to your target PDF (e.g. `MyBook.pdf`), record bookmark titles and page
17-
numbers of the PDF into a TEXT file (e.g. `my_bookmarks.txt`) **by hand**
18-
with the following format:
16+
1. Turn to your target PDF (e.g. `MyBook.pdf`), record bookmark titles and page
17+
numbers of the PDF into a TEXT file (e.g. `my_bookmarks.txt`) **by hand**
18+
with the following format:
1919

2020
```
2121
<nested level>"<bookmark title>"|<page number>
2222
```
2323
24-
For samples, see [`sample_bookmarks.txt`](./sample_bookmarks.txt).
24+
For samples, see [sample_bookmarks.txt](sample_bookmarks.txt). (Offsets are also
25+
supported, see [here](https://github.com/RussellLuo/pdfbookmarker/pull/7#issuecomment-711136889))
2526
26-
2) Generate a copy of `MyBook.pdf` with additional bookmarks file specified by
27-
`my_bookmarks.txt`:
27+
2. Generate a copy of `MyBook.pdf` with additional bookmarks file specified by
28+
`my_bookmarks.txt`:
2829
2930
```bash
3031
$ pdfbm MyBook.pdf my_bookmarks.txt
3132
```
3233
33-
An auto-detected, expected or suggested filename for bookmarks is `MyBook.txt`,
34-
when the input filenamne e.g. is `MyBook.pdf`.
35-
36-
The default filename of the output PDF is `MyBook-new.pdf`; You, of course, can
37-
specify the filename explicitly:
38-
39-
```bash
40-
$ pdfbm MyBook.pdf my_bookmarks.txt MyBook_with_bookmarks.pdf
41-
```
34+
An auto-detected, expected or suggested filename for bookmarks is `MyBook.txt`,
35+
when the input filenamne e.g. is `MyBook.pdf`.
36+
37+
The default filename of the output PDF is `MyBook-new.pdf`; You, of course, can
38+
specify the filename explicitly:
39+
40+
```bash
41+
$ pdfbm MyBook.pdf my_bookmarks.txt MyBook_with_bookmarks.pdf
42+
```
4243
4344
4445
## License

pdfbookmarker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,21 @@ def get_bookmarks_tree(bookmarks_filename):
120120
offset = 0
121121
prev_level = 0
122122
for line in codecs.open(bookmarks_filename, 'r', encoding='utf-8'):
123-
if line[0:2] == '//':
123+
line = line.strip()
124+
if line.startswith('//'):
124125
try:
125126
offset = int(line[2:])
126127
except ValueError:
127128
pass
128129
continue
129-
res = re.match(r'(\+*)\s*?"([^"]+)"\s*\|\s*(\d+)', line.strip())
130+
res = re.match(r'(\+*)\s*?"([^"]+)"\s*\|\s*(\d+)', line)
130131
if res:
131132
pluses, title, page_num = res.groups()
132133
cur_level = len(pluses) # plus count stands for level
133134
cur_node = (title, int(page_num) - 1 + offset, [])
134135

135136
if not (0 < cur_level <= prev_level + 1):
136-
raise Exception('plus (+) count is invalid here: %s' % line.strip())
137+
raise Exception('plus (+) count is invalid here: %s' % line)
137138
else:
138139
# append the current node into its parent node (with the level `cur_level` - 1)
139140
latest_nodes[cur_level - 1].append(cur_node)

0 commit comments

Comments
 (0)