Skip to content

Commit

Permalink
Zjednodušeny transformační skripty
Browse files Browse the repository at this point in the history
  • Loading branch information
pepr committed Mar 27, 2012
1 parent ac41538 commit 0ff54d0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 71 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,6 @@ PonormeSeDoPythonu3/
buildPDF/
*~
*.zip
changelog.html
changelog.html
PonormeSeDoPythonu3single.html
PonormeSeDoPythonu3.pdf
4 changes: 3 additions & 1 deletion clean.bat
@@ -1,4 +1,6 @@
del *.zip
del *~
del changelog.html
del PonormeSeDoPythonu3single.html
del PonormeSeDoPythonu3.pdf
rmdir /S /Q buildPDF
rmdir /S /Q PonormeSeDoPythonu3
8 changes: 4 additions & 4 deletions pack.bat
@@ -1,6 +1,6 @@
zip -r PonormeSeDoPythonu3 PonormeSeDoPythonu3
pushd buildPDF
zip -r PonormeSeDoPythonu3pdf PonormeSeDoPythonu3.pdf
move PonormeSeDoPythonu3pdf.zip ..
copy PonormeSeDoPythonu3.pdf ..
copy single.html PonormeSeDoPythonu3single.html
popd
call ziphtml.bat
rem Přibalí i vygenerovaný changelog.html a PonormeSeDoPythonu3single.html
zip -r PonormeSeDoPythonu3-html *.html *.css examples/* i/* j/*
9 changes: 6 additions & 3 deletions util/buildchangelog.py
Expand Up @@ -33,7 +33,7 @@
<p class=c>Generováno pro český překlad.
<script src=j/jquery.js></script>
<script src=j/dip3.js></script>
<script src=j/dip3.js></script>
'''

pgmPath = os.path.split(os.path.realpath(__file__))[0]
Expand All @@ -48,10 +48,13 @@ def gitLogLines():
line = line.decode('utf-8')
except UnicodeDecodeError:
line = line.decode('cp1250') # první git commit byl v cp1250
yield line.replace('<', '&lt;')
yield line

# Generovaný výstupní soubor.
with open(chlogname, 'w', encoding='utf-8', newline='\n') as f:
f.write(head)
f.write(''.join(gitLogLines()).replace('&', '&amp;'))
content = ''.join(gitLogLines())
content = content.replace('&', '&amp;')
content = content.replace('<', '&lt;')
f.write(content)
f.write(foot)
91 changes: 29 additions & 62 deletions util/flatten2.py
Expand Up @@ -11,52 +11,19 @@
utilDir = os.path.split(os.path.realpath(__file__))[0]
srcDir = os.path.realpath(os.path.join(utilDir, '..'))
buildPDFDir = os.path.realpath(os.path.join(utilDir, '..', 'buildPDF'))
buildHTMLDir = os.path.realpath(os.path.join(utilDir, '..', 'PonormeSeDoPythonu3'))


def srcGen(filename):
with open(filename, encoding='utf-8') as f:
content = f.read()

# Rectify some of the problems generated by MemoQ in the translated
# content (some newlines were somehow replaced by a space).
content = content.replace(' <p', '\n<p')
content = content.replace(' <h', '\n<h')
content = content.replace(' <li', '\n<li')
content = content.replace(' <div', '\n<div')
content = content.replace(' </div', '\n</div')

# Tohle narovnává blbě zauvozovkovaný atribut, který byl původně unquoted.
# Pozorováno to bylo u <a href=xyz/>, kde koncové lomítko vyjadřuje adresář.
# Zde se řeší jen speciální případ.
ahrefRex = re.compile(r'<a\s+href=("\S*?)" />')
content = ahrefRex.sub(r'<a href=\1">', content)

# Write the corrected content to the file with the same name, but to
# the buildDir (nasty side effect only to get the file for checking
# of what could cause problems in the next flattening process).
fname = os.path.join(buildHTMLDir, os.path.basename(filename))
f = open(fname, 'w', encoding='utf-8')
f.write(content)
f.close()

# Yield the lines of the content as if they were read from a text file.
for line in content.splitlines(True): # keep ends
yield line
print('Corrected HTML', repr(os.path.basename(filename)))



def copyNames(srcDir, dstDir, nameLst):
"""Okopíruje jména z nameLst z adresáře srcDir do dstDir.
Pokud destDir neexistuje, bude nejdříve vytvořen. Pokud jméno
z nameLst odpovídá podadresáři, bude případný existující cílový
z nameLst odpovídá podadresáři, bude případný existující cílový
podadresář nejdříve smazán a okopírován celý zdrojový znovu.
"""
# Pokud neexistuje cílový adresář, vyrobíme jej.
if not os.path.isdir(dstDir):
os.makedirs(dstDir)

for name in nameLst:
src = os.path.join(srcDir, name)
dst = os.path.join(dstDir, name)
Expand All @@ -68,7 +35,7 @@ def copyNames(srcDir, dstDir, nameLst):
shutil.copytree(src, dst)
elif os.path.isfile(src):
shutil.copy2(src, dst)


# Set list of chapters in the order defined by the index.html. Append also
# the files that are not there (about, colophon).
Expand Down Expand Up @@ -103,7 +70,6 @@ def copyNames(srcDir, dstDir, nameLst):
]

# Připravíme cílové adresáře pro HTML a PDF (kopie potřebného).
copyNames(srcDir, buildHTMLDir, ['i', 'j', 'examples', 'dip3.css'])
copyNames(utilDir, buildPDFDir, ['dip3.css', 'prince.css'])
copyNames(srcDir, buildPDFDir, ['i', 'examples'])

Expand All @@ -127,7 +93,7 @@ def copyNames(srcDir, dstDir, nameLst):
fname = os.path.join(utilDir, 'single-footer.html')
with open(fname, encoding='utf-8') as f:
footer = f.read()

singleName = os.path.join(buildPDFDir, 'single.html')
with open(singleName, 'w', encoding='utf-8') as out:
out.write(header) # Header before the style definitions.
Expand Down Expand Up @@ -157,27 +123,28 @@ def copyNames(srcDir, dstDir, nameLst):
chapter_id = "chapter-" + id_suffix
filename = os.path.join(srcDir, fname)
out.write("<div id={0}>\n".format(chapter_id))
for line in srcGen(filename):
if line.startswith('<body id='):
line = line.replace('<body id=', '<div id=')
out.write(line)
elif line.startswith('</body>'):
line = line.replace('</body>', '</div>')
out.write(line)

if line.count('<h1>'):
include = True
if line.count('<p class=v') or line.count('<p class=c>&copy;') or line.count('<p class=c>©'):
include = False
if line.count('<p id=toc'):
line = line.replace(' id=toc', '')
if include:
# fix cross-references
line = id_munge.sub(lambda x: " id={0}-{1}>".format(id_suffix, x.group(1)), line)
line = same_chapter_fragment_href.sub(lambda x: "<a href=#{0}-{1}".format(id_suffix, x.group(1)), line)
line = chapter_href.sub(lambda x: "<a href=#chapter-" + x.group(1).replace('.html', '') + ">", line)
line = chapter_fragment_href.sub(lambda x: "<a href=#{0}-{1}>".format(x.group(1).replace('.html', ''), x.group(2)) , line)
out.write(line)
with open(filename, encoding='utf-8') as f:
for line in f:
if line.startswith('<body id='):
line = line.replace('<body id=', '<div id=')
out.write(line)
elif line.startswith('</body>'):
line = line.replace('</body>', '</div>')
out.write(line)

if line.count('<h1>'):
include = True
if line.count('<p class=v') or line.count('<p class=c>&copy;') or line.count('<p class=c>©'):
include = False
if line.count('<p id=toc'):
line = line.replace(' id=toc', '')
if include:
# fix cross-references
line = id_munge.sub(lambda x: " id={0}-{1}>".format(id_suffix, x.group(1)), line)
line = same_chapter_fragment_href.sub(lambda x: "<a href=#{0}-{1}".format(id_suffix, x.group(1)), line)
line = chapter_href.sub(lambda x: "<a href=#chapter-" + x.group(1).replace('.html', '') + ">", line)
line = chapter_fragment_href.sub(lambda x: "<a href=#{0}-{1}>".format(x.group(1).replace('.html', ''), x.group(2)) , line)
out.write(line)
out.write("</div>\n")


Expand All @@ -189,7 +156,7 @@ def copyNames(srcDir, dstDir, nameLst):
programFiles = os.getenv('ProgramFiles(x86)')
if programFiles is None:
programFiles = os.getenv('ProgramFiles')

prince = os.path.join(programFiles, 'Prince', 'Engine', 'bin', 'Prince.exe')
prince = os.path.normpath(prince)

Expand Down

0 comments on commit 0ff54d0

Please sign in to comment.