From 8de3f8d487062e2f10638d6dd82c2002e0ec15d2 Mon Sep 17 00:00:00 2001 From: meles5 Date: Tue, 27 Oct 2015 22:24:28 +0100 Subject: [PATCH 1/6] Improved script --- scripts/asciidoc2html.py | 59 ++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index e6cffae51..a34b1e819 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -110,30 +110,49 @@ class AsciiDoc: modified_src = os.path.join(self._tempdir, src_basename) shutil.copy('www/header.asciidoc', modified_src) + final_source = "" + + with open(src, 'r', encoding='utf-8') as infp: + final_source += "\n\n" + hidden = False + found_title = False + title = "" + last_line = "" + + for line in infp: + if line.strip() == '// QUTE_WEB_HIDE': + assert not hidden + hidden = True + elif line.strip() == '// QUTE_WEB_HIDE_END': + assert hidden + hidden = False + + if not found_title: + if re.match(r'^=+$', line): + line = line.replace('=', '-') + found_title = True + title = last_line + "=" * (len(last_line) - 1) + elif re.match(r'^= .+', line): + line = '==' + line[1:] + found_title = True + title = last_line + "=" * (len(last_line) - 1) + + if not hidden: + final_source += line.replace(".asciidoc[", ".html[") + last_line = line + with open(modified_src, 'a', encoding='utf-8') as outfp: - with open(src, 'r', encoding='utf-8') as infp: - outfp.write('\n') - hidden = False - replaced_title = False + outfp.write(final_source) - for line in infp: - if line.strip() == '// QUTE_WEB_HIDE': - assert not hidden - hidden = True - elif line.strip() == '// QUTE_WEB_HIDE_END': - assert hidden - hidden = False + current = open(modified_src) + current_lines = current.read() + current.close() - if not replaced_title: - if re.match(r'^=+$', line): - line = line.replace('=', '-') - replaced_title = True - elif re.match(r'^= .+', line): - line = '==' + line[1:] - replaced_title = True + final_version = open(modified_src, "w+") + final_version.write(title + "\n\n" + current_lines) + final_version.close() - if not hidden: - outfp.write(line) + print(title) self.call(modified_src, dst, '--theme=qute') From 8600acddb11ccbf1b6db1cdbb4de805e56837073 Mon Sep 17 00:00:00 2001 From: meles5 Date: Thu, 29 Oct 2015 15:41:57 +0100 Subject: [PATCH 2/6] Moved everything into one block and used with to open files --- scripts/asciidoc2html.py | 70 ++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index a34b1e819..c06608c01 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -110,49 +110,41 @@ class AsciiDoc: modified_src = os.path.join(self._tempdir, src_basename) shutil.copy('www/header.asciidoc', modified_src) - final_source = "" - - with open(src, 'r', encoding='utf-8') as infp: - final_source += "\n\n" - hidden = False - found_title = False - title = "" - last_line = "" - - for line in infp: - if line.strip() == '// QUTE_WEB_HIDE': - assert not hidden - hidden = True - elif line.strip() == '// QUTE_WEB_HIDE_END': - assert hidden - hidden = False - - if not found_title: - if re.match(r'^=+$', line): - line = line.replace('=', '-') - found_title = True - title = last_line + "=" * (len(last_line) - 1) - elif re.match(r'^= .+', line): - line = '==' + line[1:] - found_title = True - title = last_line + "=" * (len(last_line) - 1) - - if not hidden: - final_source += line.replace(".asciidoc[", ".html[") - last_line = line - with open(modified_src, 'a', encoding='utf-8') as outfp: - outfp.write(final_source) + with open(src, 'r', encoding='utf-8') as infp: + outfp.write("\n\n") + hidden = False + found_title = False + title = "" + last_line = "" - current = open(modified_src) - current_lines = current.read() - current.close() + for line in infp: + if line.strip() == '// QUTE_WEB_HIDE': + assert not hidden + hidden = True + elif line.strip() == '// QUTE_WEB_HIDE_END': + assert hidden + hidden = False - final_version = open(modified_src, "w+") - final_version.write(title + "\n\n" + current_lines) - final_version.close() + if not found_title: + if re.match(r'^=+$', line): + line = line.replace('=', '-') + found_title = True + title = last_line + "=" * (len(last_line) - 1) + elif re.match(r'^= .+', line): + line = '==' + line[1:] + found_title = True + title = last_line + "=" * (len(last_line) - 1) - print(title) + if not hidden: + outfp.write(line.replace(".asciidoc[", ".html[")) + last_line = line + + with open(modified_src) as current: + current_lines = current.read() + + with open(modified_src, "w+") as final_version: + final_version.write(title + "\n\n" + current_lines) self.call(modified_src, dst, '--theme=qute') From f807842a52b2120aa31d1f7bc04d01c7adad9857 Mon Sep 17 00:00:00 2001 From: meles5 Date: Thu, 29 Oct 2015 16:30:25 +0100 Subject: [PATCH 3/6] Improved code style --- scripts/asciidoc2html.py | 63 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index c06608c01..91f3ba656 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -29,6 +29,7 @@ import glob import shutil import tempfile import argparse +import io sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir)) @@ -110,41 +111,45 @@ class AsciiDoc: modified_src = os.path.join(self._tempdir, src_basename) shutil.copy('www/header.asciidoc', modified_src) - with open(modified_src, 'a', encoding='utf-8') as outfp: - with open(src, 'r', encoding='utf-8') as infp: - outfp.write("\n\n") - hidden = False - found_title = False - title = "" - last_line = "" + outfp = io.StringIO() - for line in infp: - if line.strip() == '// QUTE_WEB_HIDE': - assert not hidden - hidden = True - elif line.strip() == '// QUTE_WEB_HIDE_END': - assert hidden - hidden = False + with open(modified_src) as header_file: + header = header_file.read() + header += "\n\n" - if not found_title: - if re.match(r'^=+$', line): - line = line.replace('=', '-') - found_title = True - title = last_line + "=" * (len(last_line) - 1) - elif re.match(r'^= .+', line): - line = '==' + line[1:] - found_title = True - title = last_line + "=" * (len(last_line) - 1) + with open(src, 'r', encoding='utf-8') as infp: + outfp.write("\n\n") + hidden = False + found_title = False + title = "" + last_line = "" - if not hidden: - outfp.write(line.replace(".asciidoc[", ".html[")) - last_line = line + for line in infp: + if line.strip() == '// QUTE_WEB_HIDE': + assert not hidden + hidden = True + elif line.strip() == '// QUTE_WEB_HIDE_END': + assert hidden + hidden = False - with open(modified_src) as current: - current_lines = current.read() + if not found_title: + if re.match(r'^=+$', line): + line = line.replace('=', '-') + found_title = True + title = last_line + "=" * (len(last_line) - 1) + elif re.match(r'^= .+', line): + line = '==' + line[1:] + found_title = True + title = last_line + "=" * (len(last_line) - 1) + + if not hidden: + outfp.write(line.replace(".asciidoc[", ".html[")) + last_line = line + + current_lines = outfp.getvalue() with open(modified_src, "w+") as final_version: - final_version.write(title + "\n\n" + current_lines) + final_version.write(title + "\n\n" + header + current_lines) self.call(modified_src, dst, '--theme=qute') From 1488f59d8f8b2588b2f6aa3ce7f7a19983c6cfaf Mon Sep 17 00:00:00 2001 From: meles5 Date: Thu, 29 Oct 2015 16:33:20 +0100 Subject: [PATCH 4/6] Close file --- scripts/asciidoc2html.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index 91f3ba656..7184006f3 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -147,6 +147,7 @@ class AsciiDoc: last_line = line current_lines = outfp.getvalue() + outfp.close() with open(modified_src, "w+") as final_version: final_version.write(title + "\n\n" + header + current_lines) From 7cb462ff8218138f4e38823dfa9800b474adf426 Mon Sep 17 00:00:00 2001 From: meles5 Date: Thu, 29 Oct 2015 16:52:38 +0100 Subject: [PATCH 5/6] Improved the folder-copy function --- scripts/asciidoc2html.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index 7184006f3..c1be2025d 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -154,8 +154,10 @@ class AsciiDoc: self.call(modified_src, dst, '--theme=qute') - for path in ['icons', 'doc/img']: - shutil.copytree(path, os.path.join(outdir, path)) + copy = {'icons':'icons', 'doc/img':'doc/img', 'www/media':'media/'} + + for src, dest in copy.items(): + shutil.copytree(src, os.path.join(outdir, dest)) os.symlink('README.html', os.path.join(outdir, 'index.html')) From 6d6ef1e386e9c8cfe796e6d6aff762c4e616b1f7 Mon Sep 17 00:00:00 2001 From: meles5 Date: Fri, 30 Oct 2015 14:52:09 +0100 Subject: [PATCH 6/6] tox fixes --- MANIFEST.in | 1 + scripts/asciidoc2html.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 1e7730676..8b987bac6 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -17,6 +17,7 @@ include requirements.txt include tox.ini include qutebrowser.py +prune www prune scripts/dev exclude scripts/asciidoc2html.py exclude doc/notes diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index c1be2025d..05c03401e 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -77,6 +77,7 @@ class AsciiDoc: self._build_docs() def _build_docs(self): + """Build the documentation by rendering .asciidoc files to .html sites.""" files = self.FILES[:] for src in glob.glob('doc/help/*.asciidoc'): name, _ext = os.path.splitext(os.path.basename(src)) @@ -86,6 +87,7 @@ class AsciiDoc: self.call(src, dst) def _build_website(self): + """Prepare and build the website.""" theme_file = os.path.abspath(os.path.join('www', 'qute.css')) shutil.copy(theme_file, self._themedir) @@ -113,7 +115,7 @@ class AsciiDoc: outfp = io.StringIO() - with open(modified_src) as header_file: + with open(modified_src, 'r', encoding='utf-8') as header_file: header = header_file.read() header += "\n\n" @@ -149,12 +151,12 @@ class AsciiDoc: current_lines = outfp.getvalue() outfp.close() - with open(modified_src, "w+") as final_version: + with open(modified_src, 'w+', encoding='utf-8') as final_version: final_version.write(title + "\n\n" + header + current_lines) self.call(modified_src, dst, '--theme=qute') - copy = {'icons':'icons', 'doc/img':'doc/img', 'www/media':'media/'} + copy = {'icons': 'icons', 'doc/img': 'doc/img', 'www/media': 'media/'} for src, dest in copy.items(): shutil.copytree(src, os.path.join(outdir, dest))