Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ ENV/

# Rope project settings
.ropeproject

tmp/
Binary file added docs/PenseEmPython.epub
Binary file not shown.
4 changes: 4 additions & 0 deletions docs/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<dc:title>Pense em Python</dc:title>
<dc:creator>Allen Downey</dc:creator>
<dc:rights>Creative Commons</dc:rights>
<dc:language>pt-BR</dc:language>
58 changes: 58 additions & 0 deletions util/epub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
from glob import glob
from shutil import rmtree
from subprocess import call
from os import path, makedirs

def _project_path():
return path.dirname(path.dirname(path.abspath(__file__)))


def _get_path(p):
return path.join(_project_path(), p)


def _fix_images_url(files, tmp_dir):
""" Corrige as urls das imagens.
Por algum motivo o pandoc não estava conseguindo baixar as imagens do site para embedar no epub.
Com esse código eu utilizo gero arquivos temporários com as essas referências acertadas para a mesma imagem dentro do projeto.
"""
makedirs(tmp_dir)
fixed_files = []
fixed_path = path.relpath(_project_path())
for fp in files:
file_name = path.split(fp)[1]
output = path.join(tmp_dir, file_name)
with open(fp) as input_file:
fixed_str = input_file.read().replace('https://github.com/PenseAllen/PensePython2e/raw/master', fixed_path)
with open(output, 'w') as output_file:
output_file.write(fixed_str)
fixed_files.append(output)
return fixed_files



def main():
glob_filter = _get_path('docs/*.md')
capa = _get_path('img/Capa_PenseEmPython332x461-borda.png')
metadata = _get_path('docs/metadata.xml')
output = _get_path('docs/PenseEmPython.epub')
files = glob(glob_filter)
files.sort()
files.pop()
tmp_dir = _get_path('tmp')
files = _fix_images_url(files, tmp_dir)
# Monta os argumentos do pandoc
args = ['pandoc', '--epub-cover-image=' + capa, '--epub-metadata=' + metadata]
args.extend(files)
args.append('-o')
args.append(output)
# Executa o pandoc
call(args)
# Remove arquivos temporários
rmtree(tmp_dir)
print('Gerado epub em', output)


if __name__ == '__main__':
main()