cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblot...@apache.org
Subject [1/7] docs commit: Renaming toc generator.
Date Wed, 05 Aug 2015 08:47:40 GMT
Repository: cordova-docs
Updated Branches:
  refs/heads/cordova-website 743d4d584 -> 85d578f84


Renaming toc generator.


Project: http://git-wip-us.apache.org/repos/asf/cordova-docs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-docs/commit/9c55eeec
Tree: http://git-wip-us.apache.org/repos/asf/cordova-docs/tree/9c55eeec
Diff: http://git-wip-us.apache.org/repos/asf/cordova-docs/diff/9c55eeec

Branch: refs/heads/cordova-website
Commit: 9c55eeece0ebe9cc88de8401bb85f248657878e6
Parents: 743d4d5
Author: Dmitry Blotsky <dmitry.blotsky@gmail.com>
Authored: Tue Aug 4 23:44:05 2015 -0700
Committer: Dmitry Blotsky <dmitry.blotsky@gmail.com>
Committed: Tue Aug 4 23:44:05 2015 -0700

----------------------------------------------------------------------
 tools/bin/gen_toc.py |  96 -----------------------------------------
 tools/bin/toc.py     | 108 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-docs/blob/9c55eeec/tools/bin/gen_toc.py
----------------------------------------------------------------------
diff --git a/tools/bin/gen_toc.py b/tools/bin/gen_toc.py
deleted file mode 100644
index 753d657..0000000
--- a/tools/bin/gen_toc.py
+++ /dev/null
@@ -1,96 +0,0 @@
-import os
-import re
-import sys
-import yaml
-
-from util import *
-
-def is_underline(line):
-    return re.match(r'-+|=+|\*+', line) is not None
-
-def get_page_title(page_path):
-
-    title = ''
-
-    # read in the page and remove the front matter
-    with open(page_path, 'r') as page_file:
-        page = page_file.read()
-    page = strip_front_matter(page)
-
-    # look for hash-headings and underline-headings
-    lines         = page.splitlines()
-    previous_line = lines[0]
-
-    for line in lines[1:]:
-
-        if is_underline(line):
-            return previous_line.strip()
-
-        match = re.match(r'^#+(.+)$', line)
-        if match:
-            return match.group(1).strip()
-
-        previous_line = line
-
-    # look for the first <hX> heading
-    match = re.search(r'[\s]*<h\d[^>]*>([^<]+)<\/h\d>', page, re.DOTALL)
-    if match:
-        return match.group(1).strip()
-
-    if title == '':
-        page_file_name = os.path.basename(page_path)
-        return page_file_name.replace('.md', '').capitalize()
-
-def main():
-
-    src_dir  = sys.argv[1]
-
-    data_root = os.path.join(src_dir, '_data', 'toc')
-    docs_root = os.path.join(src_dir, 'docs')
-
-    # go through all languages
-    for lang_name in listdirs(docs_root):
-        lang_path = os.path.join(docs_root, lang_name)
-
-        # go through all versions
-        for version_name in listdirs(lang_path):
-            version_path = os.path.join(lang_path, version_name)
-
-            # get path for toc file
-            toc           = []
-            toc_file_name = lang_name + '-' + version_name.replace('.', '-') + '.yml'
-            toc_file_path = os.path.join(data_root, toc_file_name)
-
-            # walk the version doc tree
-            for dir_root, dir_names, page_names in os.walk(version_path):
-
-                # get the URL prefix for pages in this directory
-                page_prefix = dir_root.replace(src_dir, '')
-
-                # process each file
-                for page_name in page_names:
-
-                    if not page_name.endswith('.md'):
-                        continue
-
-                    # compute page URL
-                    page_url = os.path.join(page_prefix, page_name)
-                    page_url = page_url.replace('.md', '.html')
-
-                    # extract page title
-                    page_path  = os.path.join(dir_root, page_name)
-                    page_title = get_page_title(page_path)
-
-                    toc.append({
-                        'name': page_title,
-                        'url': page_url
-                    })
-
-            # write out the toc
-            toc_string = yaml.dump(toc)
-            with open(toc_file_path, 'w') as data_file:
-                data_file.write(toc_string)
-            print toc_file_path
-
-if __name__ == '__main__':
-    main()

http://git-wip-us.apache.org/repos/asf/cordova-docs/blob/9c55eeec/tools/bin/toc.py
----------------------------------------------------------------------
diff --git a/tools/bin/toc.py b/tools/bin/toc.py
new file mode 100644
index 0000000..ae2dc7c
--- /dev/null
+++ b/tools/bin/toc.py
@@ -0,0 +1,108 @@
+import os
+import re
+import sys
+import yaml
+import argparse
+
+from util import *
+
+def is_underline(line):
+    return re.match(r'-+|=+|\*+', line) is not None
+
+def get_page_title(page_path):
+
+    # read in the page
+    with open(page_path, 'r') as page_file:
+        page = page_file.read()
+
+    # remove the front matter
+    page = strip_front_matter(page)
+
+    # look for hash-headings and underline-headings
+    lines         = page.splitlines()
+    previous_line = lines[0]
+
+    for line in lines[1:]:
+
+        if is_underline(line):
+            return previous_line.strip()
+
+        match = re.match(r'^#+(.+)$', line)
+        if match:
+            return match.group(1).strip()
+
+        previous_line = line
+
+    # look for the first <hX> heading
+    match = re.search(r'[\s]*<h\d[^>]*>([^<]+)<\/h\d>', page, re.DOTALL)
+    if match:
+        return match.group(1).strip()
+
+    # if all else fails, use the page name
+    page_file_name = os.path.basename(page_path)
+    return page_file_name.replace('.md', '').capitalize()
+
+def generate(source_dir, prefix):
+
+    toc = []
+
+    # walk the tree
+    for dir_root, dir_names, page_names in os.walk(source_dir):
+
+        # get the URL prefix for pages in this directory
+        dir_prefix = dir_root.replace(source_dir, '')
+        url_prefix = prefix + dir_prefix
+
+        # process each file
+        for page_name in page_names:
+
+            # skip non-Markdown files
+            if not page_name.endswith('.md'):
+                continue
+
+            # compute page URL
+            page_url = os.path.join(url_prefix, page_name)
+            page_url = page_url.replace('.md', '.html')
+
+            # extract page title
+            page_path  = os.path.join(dir_root, page_name)
+            page_title = get_page_title(page_path)
+
+            toc.append({
+                'name': page_title,
+                'url': page_url
+            })
+
+    return yaml.dump(toc)
+
+def main():
+
+    # create argument parser
+    arg_parser = argparse.ArgumentParser(prog=sys.argv[0])
+    arg_parser.add_argument('source')
+    arg_parser.add_argument('-o', '--output')
+    arg_parser.add_argument('-p', '--prefix', default='')
+    args = arg_parser.parse_args()
+
+    source_dir  = args.source
+    prefix      = args.prefix
+    output_path = args.output
+
+    # generate the toc
+    toc_string = generate(source_dir, prefix)
+
+    # write out the toc
+    if output_path is not None:
+
+        output_dir = os.path.dirname(output_path)
+
+        if not os.path.exists(output_dir):
+            mkdirp(output_dir)
+
+        with open(output_path, 'w') as toc_file:
+            toc_file.write(toc_string)
+    else:
+        print toc_string
+
+if __name__ == '__main__':
+    main()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message