pdf-auto-outline 0.1.5__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pdf-auto-outline
3
- Version: 0.1.5
3
+ Version: 0.1.6
4
4
  Summary: Automatically generate and edit PDF table of contents / outline
5
5
  Author: Rossikos
6
6
  Author-email: Rossikos <216631970+rossikos@users.noreply.github.com>
@@ -82,7 +82,6 @@ The optional part can be one of:
82
82
  ```
83
83
  | None same as not including it
84
84
  | 241.2 y-ordinate
85
- | Point(72.0, 363.9) x and y coords
86
85
  | {<dictionary>} dictionary with more attributes for the ToC entry
87
86
  ```
88
87
 
@@ -92,8 +91,10 @@ Example commands; add to `prefs_user.config`.
92
91
 
93
92
  ```
94
93
  new_command _gen_toc pdfao "%{file_path}" --sioyek path/to/sioyek -mp 4
95
- new_command _edit_toc pdfao "%{file_path}" --sioyek path/to/sioyek -e
94
+ new_command _edit_toc pdfao "%{file_path}"
96
95
  ```
97
96
 
97
+ The sioyek library and flag are optional; they allow logging to the status bar. This is more useful for ToC generation where you may want a progress bar.
98
+
98
99
  If you don't wish to install from PyPI, download source and use `python3 -m path/to/src/pdf_auto_outline` in place of `pdfao`.
99
100
 
@@ -65,7 +65,6 @@ The optional part can be one of:
65
65
  ```
66
66
  | None same as not including it
67
67
  | 241.2 y-ordinate
68
- | Point(72.0, 363.9) x and y coords
69
68
  | {<dictionary>} dictionary with more attributes for the ToC entry
70
69
  ```
71
70
 
@@ -75,8 +74,10 @@ Example commands; add to `prefs_user.config`.
75
74
 
76
75
  ```
77
76
  new_command _gen_toc pdfao "%{file_path}" --sioyek path/to/sioyek -mp 4
78
- new_command _edit_toc pdfao "%{file_path}" --sioyek path/to/sioyek -e
77
+ new_command _edit_toc pdfao "%{file_path}"
79
78
  ```
80
79
 
80
+ The sioyek library and flag are optional; they allow logging to the status bar. This is more useful for ToC generation where you may want a progress bar.
81
+
81
82
  If you don't wish to install from PyPI, download source and use `python3 -m path/to/src/pdf_auto_outline` in place of `pdfao`.
82
83
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pdf-auto-outline"
3
- version = "0.1.5"
3
+ version = "0.1.6"
4
4
  description = "Automatically generate and edit PDF table of contents / outline"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -0,0 +1 @@
1
+ __version__ = '0.1.6'
@@ -1,10 +1,10 @@
1
1
  import pymupdf.layout
2
- from pymupdf import Point
3
2
  from time import perf_counter
4
3
  from multiprocessing import Pool
5
4
  import os
6
5
  import subprocess
7
6
  import argparse
7
+ import tempfile
8
8
 
9
9
  SIOYEK = None
10
10
 
@@ -148,7 +148,12 @@ def align_toc_lvls(toc_entries: list) -> list:
148
148
 
149
149
  return toc_entries
150
150
 
151
- def generate_txtfile(toc_entries, txtfile='outline.txt') -> str:
151
+ def get_tmpfile():
152
+ return tempfile.NamedTemporaryFile(
153
+ mode='w+', encoding='utf-8', delete=False, suffix='.txt'
154
+ )
155
+
156
+ def generate_txtfile(toc_entries, txtfile=get_tmpfile()):
152
157
  import textwrap
153
158
  txt = textwrap.dedent("""\
154
159
  ============================================================
@@ -168,34 +173,37 @@ def generate_txtfile(toc_entries, txtfile='outline.txt') -> str:
168
173
  txt += '\n'.join(f"{' '*4 * (i[0] - 1)}{i[1]} | {i[2]}"
169
174
  for i in toc_entries)
170
175
 
171
- with open(txtfile, 'w', encoding='utf-8') as f:
172
- f.write(txt)
176
+ txtfile.write(txt)
177
+ txtfile.flush()
178
+ txtfile.seek(0)
173
179
 
174
180
  return txtfile
175
181
 
176
182
 
177
- def parse_txtfile(txtfile='outline.txt', tablevel=2) -> list:
183
+
184
+ def parse_txtfile(f, tablevel=2) -> list:
178
185
  toc_entries = []
179
- with open(txtfile) as f:
180
- if (c := f.read(1)) == 'C':
181
- log('Outline not written')
182
- exit()
183
- elif c == '=':
184
- lines = f.readlines()[7:]
185
- else: lines = f.read()
186
-
187
- for i in lines:
188
- i = i.replace('\t', ' '*tablevel)
189
- lvl = (len(i) - len(i.lstrip())) // 4 + 1
190
- a = i.lstrip().split(' | ')
191
- if len(a) < 3:
192
- toc_entries.append(
193
- [lvl, a[0], int(a[1])]
194
- )
195
- else:
196
- toc_entries.append(
197
- [lvl, a[0], int(a[1]), eval(a[2])]
198
- )
186
+ if (c := f.read(1)) == 'C':
187
+ log('Outline not written')
188
+ exit()
189
+ elif c == '=':
190
+ lines = f.readlines()[7:]
191
+ else: lines = f.read()
192
+
193
+ for i in lines:
194
+ i = i.replace('\t', ' '*tablevel)
195
+ lvl = (len(i) - len(i.lstrip())) // 4 + 1
196
+ a = i.lstrip().split(' | ')
197
+ if len(a) < 3:
198
+ toc_entries.append(
199
+ [lvl, a[0], int(a[1])]
200
+ )
201
+ else:
202
+ toc_entries.append(
203
+ [lvl, a[0], int(a[1]), eval(a[2])]
204
+ )
205
+
206
+ f.close()
199
207
 
200
208
  return toc_entries
201
209
 
@@ -213,15 +221,12 @@ def get_toc_custom(doc) -> list:
213
221
  toc_entries = [[*i[:3], i[3].get('to')[1]] for i in doc.get_toc(False)]
214
222
  return toc_entries
215
223
 
216
- def edit_txtfile(txtfile='outline.txt'):
217
- # editor = os.environ.get('EDITOR', 'notepad' if os.name == 'nt' else 'vi')
218
- # editor = os.environ.get('EDITOR', 'start' if os.name == 'nt' else 'xdg-open')
219
- name = os.name
220
- if name == 'nt':
221
- subprocess.run(['start', '/WAIT', txtfile], shell=True)
224
+ def edit_txtfile(f):
225
+ if os.name == 'nt':
226
+ subprocess.run(['start', '/WAIT', f.name], shell=True)
222
227
  else: # name == 'posix':
223
228
  editor = os.environ.get('EDITOR', 'vi')
224
- subprocess.run([editor, txtfile])
229
+ subprocess.run([editor, f.name])
225
230
 
226
231
  def main():
227
232
  parser = argparse.ArgumentParser(prog='pdfao')
@@ -234,19 +239,19 @@ def main():
234
239
  parser.add_argument('-i', '--infile', type=str, metavar='<file>', help='write toc from file to pdf')
235
240
  parser.add_argument('-t', '--tablevel', type=int, metavar='<n>', help='tab = n toc nesting levels (default 2)', default=2)
236
241
  parser.add_argument('--sioyek', type=str, metavar='<path>', help='for users of the Sioyek pdf viewer')
237
- parser.add_argument('--version', action='version', version='%(prog)s 0.1.5')
242
+ parser.add_argument('--version', action='version', version='%(prog)s 0.1.6')
238
243
 
239
244
  args = parser.parse_args()
240
245
 
246
+ if args.out:
247
+ args.out = os.path.join(
248
+ os.path.dirname(args.filename),
249
+ args.out)
250
+
241
251
  if args.sioyek:
242
252
  from sioyek.sioyek import Sioyek
243
253
  global SIOYEK
244
254
  SIOYEK = Sioyek(args.sioyek)
245
- if args.out:
246
- args.out = os.path.join(
247
- os.path.dirname(args.filename),
248
- args.out
249
- )
250
255
  # local_db = args.sioyek[1]
251
256
  # shared_db = args.sioyek[2]
252
257
  # pdf_path = args.sioyek[3]
@@ -255,14 +260,15 @@ def main():
255
260
  if args.edit or args.superedit:
256
261
  doc = pymupdf.Document(args.filename)
257
262
  if args.superedit:
258
- generate_txtfile(doc.get_toc(False))
263
+ f = generate_txtfile(doc.get_toc(False))
259
264
  else:
260
- generate_txtfile(get_toc_custom(doc))
261
- edit_txtfile()
262
- toc_entries = parse_txtfile(tablevel=args.tablevel)
265
+ f = generate_txtfile(get_toc_custom(doc))
266
+ edit_txtfile(f)
267
+ toc_entries = parse_txtfile(f, args.tablevel)
263
268
  embed_toc(args.filename, toc_entries, args.out)
269
+ os.remove(f.name)
264
270
  elif args.infile:
265
- toc_entries = parse_txtfile(args.infile, args.tablevel)
271
+ toc_entries = parse_txtfile(open(args.infile, encoding='utf-8'), args.tablevel)
266
272
  embed_toc(args.filename, toc_entries, args.out)
267
273
  else: # generate toc
268
274
  start = perf_counter()
@@ -274,10 +280,11 @@ def main():
274
280
  if args.straight:
275
281
  embed_toc(args.filename, toc_entries, args.out)
276
282
  else:
277
- generate_txtfile(toc_entries)
278
- edit_txtfile()
279
- toc_entries = parse_txtfile(tablevel=args.tablevel)
283
+ f = generate_txtfile(toc_entries)
284
+ edit_txtfile(f)
285
+ toc_entries = parse_txtfile(f, args.tablevel)
280
286
  embed_toc(args.filename, toc_entries, args.out)
287
+ os.remove(f.name)
281
288
 
282
289
  # if args.sioyek and not args.out:
283
290
  # to_hash = get_md5_hash(args.filename)
@@ -0,0 +1,4 @@
1
+ txt = "aa\nbb\ncc"
2
+
3
+ for i in txt:
4
+ print(i)
@@ -1 +0,0 @@
1
- __version__ = '0.1.5'