Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5dcb1bd

Browse filesBrowse files
authored
Merge pull request PenseAllen#2 from diegorocha/master
Incluido epub e script de geração com pandoc
2 parents c910c69 + 74df385 commit 5dcb1bd
Copy full SHA for 5dcb1bd

File tree

Expand file treeCollapse file tree

4 files changed

+64
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+64
-0
lines changed
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboardExpand all lines: .gitignore
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
tmp/
Collapse file

‎docs/PenseEmPython.epub‎

Copy file name to clipboard
806 KB
Binary file not shown.
Collapse file

‎docs/metadata.xml‎

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<dc:title>Pense em Python</dc:title>
2+
<dc:creator>Allen Downey</dc:creator>
3+
<dc:rights>Creative Commons</dc:rights>
4+
<dc:language>pt-BR</dc:language>
Collapse file

‎util/epub.py‎

Copy file name to clipboard
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
from glob import glob
3+
from shutil import rmtree
4+
from subprocess import call
5+
from os import path, makedirs
6+
7+
def _project_path():
8+
return path.dirname(path.dirname(path.abspath(__file__)))
9+
10+
11+
def _get_path(p):
12+
return path.join(_project_path(), p)
13+
14+
15+
def _fix_images_url(files, tmp_dir):
16+
""" Corrige as urls das imagens.
17+
Por algum motivo o pandoc não estava conseguindo baixar as imagens do site para embedar no epub.
18+
Com esse código eu utilizo gero arquivos temporários com as essas referências acertadas para a mesma imagem dentro do projeto.
19+
"""
20+
makedirs(tmp_dir)
21+
fixed_files = []
22+
fixed_path = path.relpath(_project_path())
23+
for fp in files:
24+
file_name = path.split(fp)[1]
25+
output = path.join(tmp_dir, file_name)
26+
with open(fp) as input_file:
27+
fixed_str = input_file.read().replace('https://github.com/PenseAllen/PensePython2e/raw/master', fixed_path)
28+
with open(output, 'w') as output_file:
29+
output_file.write(fixed_str)
30+
fixed_files.append(output)
31+
return fixed_files
32+
33+
34+
35+
def main():
36+
glob_filter = _get_path('docs/*.md')
37+
capa = _get_path('img/Capa_PenseEmPython332x461-borda.png')
38+
metadata = _get_path('docs/metadata.xml')
39+
output = _get_path('docs/PenseEmPython.epub')
40+
files = glob(glob_filter)
41+
files.sort()
42+
files.pop()
43+
tmp_dir = _get_path('tmp')
44+
files = _fix_images_url(files, tmp_dir)
45+
# Monta os argumentos do pandoc
46+
args = ['pandoc', '--epub-cover-image=' + capa, '--epub-metadata=' + metadata]
47+
args.extend(files)
48+
args.append('-o')
49+
args.append(output)
50+
# Executa o pandoc
51+
call(args)
52+
# Remove arquivos temporários
53+
rmtree(tmp_dir)
54+
print('Gerado epub em', output)
55+
56+
57+
if __name__ == '__main__':
58+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.