txt2ebook 0.1.118__tar.gz → 0.1.120__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.
Files changed (52) hide show
  1. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/PKG-INFO +1 -1
  2. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/pyproject.toml +1 -1
  3. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/__init__.py +16 -15
  4. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/cli.py +1 -30
  5. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/parser.py +8 -5
  6. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/__init__.py +1 -2
  7. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/env.py +3 -3
  8. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/massage.py +101 -4
  9. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/parse.py +1 -3
  10. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/tokenizer.py +4 -1
  11. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/txt2ebook.py +6 -1
  12. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/LICENSE.md +0 -0
  13. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/README.md +0 -0
  14. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/__main__.py +0 -0
  15. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/exceptions.py +0 -0
  16. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/__init__.py +0 -0
  17. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/base.py +0 -0
  18. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/epub.py +0 -0
  19. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/gmi.py +0 -0
  20. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/md.py +0 -0
  21. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/pdf.py +0 -0
  22. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/templates/__init__.py +0 -0
  23. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/templates/epub/__init__.py +0 -0
  24. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/templates/epub/clean.css +0 -0
  25. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/templates/epub/condense.css +0 -0
  26. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/templates/epub/noindent.css +0 -0
  27. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/tex.py +0 -0
  28. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/txt.py +0 -0
  29. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/formats/typ.py +0 -0
  30. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/helpers/__init__.py +0 -0
  31. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/languages/__init__.py +0 -0
  32. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/languages/en.py +0 -0
  33. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/languages/zh_cn.py +0 -0
  34. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/languages/zh_tw.py +0 -0
  35. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/en/LC_MESSAGES/txt2ebook.mo +0 -0
  36. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/en/LC_MESSAGES/txt2ebook.po +0 -0
  37. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/txt2ebook.pot +0 -0
  38. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/zh-cn/LC_MESSAGES/txt2ebook.mo +0 -0
  39. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/zh-cn/LC_MESSAGES/txt2ebook.po +0 -0
  40. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/zh-tw/LC_MESSAGES/txt2ebook.mo +0 -0
  41. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/locales/zh-tw/LC_MESSAGES/txt2ebook.po +0 -0
  42. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/models/__init__.py +0 -0
  43. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/models/book.py +0 -0
  44. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/models/chapter.py +0 -0
  45. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/models/volume.py +0 -0
  46. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/epub.py +0 -0
  47. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/gmi.py +0 -0
  48. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/md.py +0 -0
  49. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/pdf.py +0 -0
  50. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/tex.py +0 -0
  51. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/subcommands/typ.py +0 -0
  52. {txt2ebook-0.1.118 → txt2ebook-0.1.120}/src/txt2ebook/zh_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: txt2ebook
3
- Version: 0.1.118
3
+ Version: 0.1.120
4
4
  Summary: CLI tool to convert txt file to ebook format
5
5
  Home-page: https://github.com/kianmeng/txt2ebook
6
6
  License: AGPL-3.0-or-later
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "txt2ebook"
3
- version = "0.1.118"
3
+ version = "0.1.120"
4
4
  description = "CLI tool to convert txt file to ebook format"
5
5
  authors = ["Kian-Meng Ang <kianmeng@cpan.org>"]
6
6
  license = "AGPL-3.0-or-later"
@@ -24,29 +24,30 @@ import langdetect
24
24
 
25
25
  logger = logging.getLogger(__name__)
26
26
 
27
- __version__ = "0.1.118"
27
+ __version__ = "0.1.120"
28
28
 
29
29
 
30
30
  def setup_logger(config: argparse.Namespace) -> None:
31
- """Configure the global logger.
31
+ """Sets up logging configuration based on command-line arguments.
32
32
 
33
33
  Args:
34
- config(argparse.Namespace): Config that contains arguments
34
+ config (argparse.Namespace): Namespace containing parsed arguments.
35
35
  """
36
36
  if config.quiet:
37
37
  logging.disable(logging.NOTSET)
38
- else:
39
- logformat = {
40
- True: "%(levelname)5s: %(message)s",
41
- False: "%(message)s",
42
- }
43
-
44
- logging.basicConfig(
45
- level=config.debug and logging.DEBUG or logging.INFO,
46
- stream=sys.stdout,
47
- format=logformat[config.debug],
48
- datefmt="%Y-%m-%d %H:%M:%S",
49
- )
38
+ return
39
+
40
+ level = logging.DEBUG if config.debug else logging.INFO
41
+ format_string = (
42
+ "%(levelname)5s: %(message)s" if config.debug else "%(message)s"
43
+ )
44
+
45
+ logging.basicConfig(
46
+ level=level,
47
+ format=format_string,
48
+ stream=sys.stdout,
49
+ datefmt="%Y-%m-%d %H:%M:%S",
50
+ )
50
51
 
51
52
 
52
53
  def log_or_raise_on_warning(msg: str, raise_on_warning: bool = False) -> None:
@@ -32,7 +32,7 @@ logger = logging.getLogger(__name__)
32
32
 
33
33
 
34
34
  def build_parser(
35
- args: Optional[Sequence[str]] = [],
35
+ _args: Optional[Sequence[str]] = None,
36
36
  ) -> argparse.ArgumentParser:
37
37
  """Build the CLI parser."""
38
38
  parser = argparse.ArgumentParser(
@@ -64,15 +64,6 @@ def build_parser(
64
64
  ),
65
65
  )
66
66
 
67
- parser.add_argument(
68
- "-t",
69
- "--title",
70
- dest="title",
71
- default=None,
72
- help="title of the ebook (default: '%(default)s')",
73
- metavar="TITLE",
74
- )
75
-
76
67
  parser.add_argument(
77
68
  "-l",
78
69
  "--language",
@@ -82,26 +73,6 @@ def build_parser(
82
73
  metavar="LANGUAGE",
83
74
  )
84
75
 
85
- parser.add_argument(
86
- "-a",
87
- "--author",
88
- dest="author",
89
- default=[],
90
- action="append",
91
- help="author of the ebook (default: '%(default)s')",
92
- metavar="AUTHOR",
93
- )
94
-
95
- parser.add_argument(
96
- "-tr",
97
- "--translator",
98
- dest="translator",
99
- default=[],
100
- action="append",
101
- help="translator of the ebook (default: '%(default)s')",
102
- metavar="TRANSLATOR",
103
- )
104
-
105
76
  parser.add_argument(
106
77
  "-fw",
107
78
  "--fullwidth",
@@ -65,7 +65,7 @@ class Parser:
65
65
  translators=translators,
66
66
  tags=tags,
67
67
  index=index,
68
- cover=self.config.cover,
68
+ cover=getattr(self.config, "cover", ""),
69
69
  raw_content=self.raw_content,
70
70
  toc=toc,
71
71
  )
@@ -216,13 +216,13 @@ class Parser:
216
216
  toc[-1].add_paragraph(token.value)
217
217
 
218
218
  # Use authors if set explicitly from command line.
219
- if self.config.author:
219
+ if hasattr(self.config, "author") and self.config.author:
220
220
  authors = self.config.author
221
221
 
222
- if self.config.title:
222
+ if hasattr(self.config, "title") and self.config.title:
223
223
  book_title = self.config.title
224
224
 
225
- if self.config.translator:
225
+ if hasattr(self.config, "translator") and self.config.translator:
226
226
  authors = self.config.translator
227
227
 
228
228
  logger.info("Found or set book title: %s", book_title)
@@ -231,7 +231,10 @@ class Parser:
231
231
  logger.info("Found or set tags: %s", repr(tags))
232
232
  logger.info("Found or set index: %s", repr(index))
233
233
 
234
- if hasattr(self.config, "sort_volume_and_chapter") and self.config.sort_volume_and_chapter:
234
+ if (
235
+ hasattr(self.config, "sort_volume_and_chapter")
236
+ and self.config.sort_volume_and_chapter
237
+ ):
235
238
  self.sort_volume_and_chapter(toc)
236
239
 
237
240
  return (book_title, authors, translators, tags, index, toc)
@@ -25,8 +25,7 @@ def build_subparser(subparsers):
25
25
 
26
26
  subcommands = {
27
27
  name: importlib.import_module(name)
28
- for finder, name, ispkg
29
- in iter_namespace
28
+ for finder, name, ispkg in iter_namespace
30
29
  }
31
30
 
32
31
  for subcommand in subcommands.values():
@@ -44,9 +44,9 @@ def run(_args: argparse.Namespace) -> None:
44
44
  None
45
45
  """
46
46
  sys_version = sys.version.replace("\n", "")
47
- print(
47
+ env = [
48
48
  f"txt2ebook: {__version__}",
49
49
  f"python: {sys_version}",
50
50
  f"platform: {platform.platform()}",
51
- sep="\n",
52
- )
51
+ ]
52
+ print(*env, sep="\n")
@@ -18,6 +18,7 @@
18
18
  import argparse
19
19
  import logging
20
20
  import sys
21
+ from importlib import import_module
21
22
 
22
23
  import cjkwrap
23
24
  import regex as re
@@ -26,8 +27,9 @@ from bs4 import UnicodeDammit
26
27
  from txt2ebook import detect_and_expect_language
27
28
  from txt2ebook.exceptions import EmptyFileError
28
29
  from txt2ebook.formats.txt import TxtWriter
30
+ from txt2ebook.models.book import Book
29
31
  from txt2ebook.parser import Parser
30
- from txt2ebook.zh_utils import zh_halfwidth_to_fullwidth
32
+ from txt2ebook.zh_utils import zh_halfwidth_to_fullwidth, zh_words_to_numbers
31
33
 
32
34
  logger = logging.getLogger(__name__)
33
35
 
@@ -55,6 +57,15 @@ def build_subparser(subparsers) -> None:
55
57
  metavar="EBOOK_FILENAME",
56
58
  )
57
59
 
60
+ massage_parser.add_argument(
61
+ "-hn",
62
+ "--header-number",
63
+ default=False,
64
+ action="store_true",
65
+ dest="header_number",
66
+ help="convert section header from words to numbers",
67
+ )
68
+
58
69
  massage_parser.add_argument(
59
70
  "-sp",
60
71
  "--split-volume-and-chapter",
@@ -126,6 +137,29 @@ def build_subparser(subparsers) -> None:
126
137
  help="short volume and chapter",
127
138
  )
128
139
 
140
+ massage_parser.add_argument(
141
+ "-op",
142
+ "--open",
143
+ default=False,
144
+ action="store_true",
145
+ dest="open",
146
+ help="open the generated file using default program",
147
+ )
148
+
149
+ massage_parser.add_argument(
150
+ "-ff",
151
+ "--filename-format",
152
+ dest="filename_format",
153
+ type=int,
154
+ default=None,
155
+ help=(
156
+ "the output filename format "
157
+ "(default: TXT_FILENAME [EBOOK_FILENAME])\n"
158
+ "1 - title_authors.EBOOK_EXTENSION\n"
159
+ "2 - authors_title.EBOOK_EXTENSION"
160
+ ),
161
+ )
162
+
129
163
  massage_parser.set_defaults(func=run)
130
164
 
131
165
 
@@ -147,10 +181,75 @@ def run(args: argparse.Namespace) -> None:
147
181
  if args.debug:
148
182
  book.debug(args.verbose)
149
183
 
184
+ if args.header_number:
185
+ book = header_number(args, book)
186
+
150
187
  writer = TxtWriter(book, args)
151
188
  writer.write()
152
189
 
153
190
 
191
+ def header_number(args: argparse.Namespace, book: Book) -> Book:
192
+ """Convert header number from words to numbers."""
193
+ stats = book.stats()
194
+
195
+ seq_lengths = {
196
+ "Volume": len(str(stats.get("Volume", 2))),
197
+ "Chapter": len(str(stats.get("Chapter", 2))),
198
+ }
199
+
200
+ for toc_item in book.toc:
201
+ toc_type = type(toc_item).__name__
202
+ if toc_type in seq_lengths:
203
+ toc_item.title = words_to_nums(
204
+ args, toc_item.title, seq_lengths[toc_type]
205
+ )
206
+
207
+ return book
208
+
209
+
210
+ def words_to_nums(args: argparse.Namespace, words: str, length: int) -> str:
211
+ """Convert header from words to numbers.
212
+
213
+ For example, `第一百零八章` becomes `第108章`.
214
+
215
+ Args:
216
+ words(str): The line that contains section header in words.
217
+ length(int): The number of left zero-padding to prepend.
218
+
219
+ Returns:
220
+ str: The formatted section header.
221
+ """
222
+ config_lang = args.language.replace("-", "_")
223
+ langconf = import_module(f"txt2ebook.languages.{config_lang}")
224
+
225
+ if args.language not in ("zh-cn", "zh-tw"):
226
+ return words
227
+
228
+ # left pad the section number if found as halfwidth integer
229
+ match = re.match(rf"第([{langconf.HALFWIDTH_NUMS}]*)", words)
230
+ if match and match.group(1) != "":
231
+ header_nums = match.group(1)
232
+ return words.replace(header_nums, str(header_nums).rjust(length, "0"))
233
+
234
+ # left pad the section number if found as fullwidth integer
235
+ match = re.match(rf"第([{langconf.FULLWIDTH_NUMS}]*)", words)
236
+ if match and match.group(1) != "":
237
+ header_nums = match.group(1)
238
+ return words.replace(header_nums, str(header_nums).rjust(length, "0"))
239
+
240
+ replaced_words = zh_words_to_numbers(words, length=length)
241
+
242
+ if args.fullwidth:
243
+ replaced_words = zh_halfwidth_to_fullwidth(replaced_words)
244
+
245
+ logger.debug(
246
+ "Convert header to numbers: %s -> %s",
247
+ words[:10],
248
+ replaced_words[:10],
249
+ )
250
+ return replaced_words
251
+
252
+
154
253
  def massage_txt(args: argparse.Namespace) -> str:
155
254
  """Massage the text file."""
156
255
  logger.info("Parsing txt file: %s", args.input_file.name)
@@ -160,9 +259,7 @@ def massage_txt(args: argparse.Namespace) -> str:
160
259
 
161
260
  content = unicode.unicode_markup
162
261
  if not content:
163
- raise EmptyFileError(
164
- f"Empty file content in {args.input_file.name}"
165
- )
262
+ raise EmptyFileError(f"Empty file content in {args.input_file.name}")
166
263
 
167
264
  content = to_unix_newline(content)
168
265
 
@@ -64,9 +64,7 @@ def run(args: argparse.Namespace) -> Book:
64
64
 
65
65
  content = unicode.unicode_markup
66
66
  if not content:
67
- raise EmptyFileError(
68
- f"Empty file content in {args.input_file.name}"
69
- )
67
+ raise EmptyFileError(f"Empty file content in {args.input_file.name}")
70
68
 
71
69
  args_language = args.language
72
70
  detect_language = detect(content)
@@ -205,7 +205,10 @@ class Tokenizer:
205
205
  rf"^{self.langconf.DEFAULT_RE_VOLUME}\s*"
206
206
  rf"{self.langconf.DEFAULT_RE_CHAPTER}"
207
207
  )
208
- if hasattr(self.config, "re_volume_chapter") and self.config.re_volume_chapter:
208
+ if (
209
+ hasattr(self.config, "re_volume_chapter")
210
+ and self.config.re_volume_chapter
211
+ ):
209
212
  re_volume_chapter = self.config.re_volume_chapter[0]
210
213
 
211
214
  match = re.search(re_volume_chapter, line)
@@ -30,7 +30,12 @@ from typing import Optional, Sequence
30
30
 
31
31
  from bs4 import UnicodeDammit
32
32
 
33
- from txt2ebook import __version__, print_env, setup_logger, detect_and_expect_language
33
+ from txt2ebook import (
34
+ __version__,
35
+ print_env,
36
+ setup_logger,
37
+ detect_and_expect_language,
38
+ )
34
39
  from txt2ebook.exceptions import EmptyFileError
35
40
  from txt2ebook.formats import (
36
41
  EBOOK_FORMATS,
File without changes
File without changes