Skip to content

Commit 6f22ac2

Browse files
committed
pandoc file breakup (epub) working
1 parent f958972 commit 6f22ac2

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

tools/ProcessEbook.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from collections import OrderedDict
1313
from betools import CmdLine, visitDir, ruler, head
1414
import webbrowser
15+
import textwrap
16+
1517

1618
ebookName = "onjava"
1719
rootPath = Path(r"C:\Users\Bruce\Dropbox\___OnJava")
@@ -92,7 +94,6 @@ def fresh_start():
9294
"""
9395
Create book build directory and copy resources into it
9496
"""
95-
# shutil.copy(str(css), str(ebookBuildPath))
9697
print("Cleaning ...")
9798
if ebookBuildPath.exists():
9899
shutil.rmtree(str(ebookBuildPath))
@@ -104,11 +105,12 @@ def _cp(src):
104105
for font in fonts:
105106
_cp(font)
106107
_cp(cover)
108+
_cp(css)
107109

108110

109111

110112
count = 0
111-
@CmdLine('r')
113+
# @CmdLine('r')
112114
def rewrite_html():
113115
"""
114116
Pre-processing HTML tagging and fixups.
@@ -226,10 +228,15 @@ def convert_to_html():
226228
def convert_to_markdown():
227229
"Convert to markdown"
228230
os.chdir(str(ebookBuildPath))
229-
# cmd = "pandoc {} -f html -t markdown -o {}.md --toc --toc-depth=2".format("onjava-3.html", "onjava")
230231
cmd = "pandoc {} -f html -t markdown -o {}.md".format("onjava-3.html", "onjava")
231232
print(cmd)
232233
os.system(cmd)
234+
with Path("onjava.md").open(encoding="utf8") as mdown:
235+
markdown = mdown.read()
236+
markdown = markdown.replace("****", "") # Clean out reduntant bolding
237+
with Path("onjava.md").open('w', encoding="utf8") as mdown:
238+
mdown.write(markdown)
239+
233240

234241
silly = r"""</div>
235242
@@ -245,12 +252,12 @@ def convert_to_markdown():
245252
`
246253
"""
247254
standalone_end_new = r"""
248-
```
249-
"""
255+
```"""
250256

251257
@CmdLine('s')
252258
def reconstruct_source_code_files():
253259
"Reconstruct source code from examples, make sure you attach output first"
260+
print("reconstruct_source_code_files")
254261
os.chdir(str(ebookBuildPath))
255262
example = re.compile(r"` //: (.*?\.(java|txt|cpp|py|prop))(.*?)///:~.*?`", re.DOTALL)
256263

@@ -311,6 +318,7 @@ def mdfilename(h1, n):
311318
chp.write("=" * len(p) + "\n")
312319
chp.write(chaps[p])
313320

321+
314322
@CmdLine('w')
315323
def view_in_texts():
316324
"Show all separate .md files in wysiwyg markdown editor"
@@ -321,10 +329,40 @@ def view_in_texts():
321329

322330
@CmdLine('e')
323331
def everything():
332+
"""Produce Markdown file from Word doc"""
324333
# fresh_start()
325334
convert_to_html()
326335
convert_to_markdown()
327336
reconstruct_source_code_files()
328337
break_up_markdown_file()
329338

339+
340+
@CmdLine('r')
341+
def reassemble_and_convert_to_epub():
342+
"""
343+
Put markdown files together, then pandoc to epub
344+
"""
345+
output_name = "onjava-assembled.md"
346+
os.chdir(str(ebookBuildPath))
347+
assembled = ""
348+
for md in Path(".").glob("[0-9][0-9]_*.md"):
349+
print(str(md))
350+
with md.open(encoding="utf8") as part:
351+
assembled += part.read() + "\n"
352+
with Path(output_name).open('w', encoding="utf8") as book:
353+
book.write(assembled)
354+
pandoc = ("pandoc {} -f markdown-native_divs -t epub -o OnJava.epub" + \
355+
" --epub-cover-image=cover.jpg " + \
356+
" --epub-embed-font=ubuntumono-r-webfont.ttf " + \
357+
" --epub-chapter-level=1 " + \
358+
" --toc-depth=2 " + \
359+
" --no-highlight " + \
360+
" --epub-stylesheet=onjava.css "
361+
).format(output_name)
362+
print(pandoc)
363+
os.system(pandoc)
364+
shutil.copy("OnJava.epub", "OnJava.zip")
365+
os.system("unzip OnJava.zip -d epub_files")
366+
367+
330368
if __name__ == '__main__': CmdLine.run()

0 commit comments

Comments
 (0)