<reponame>lingruiluo/jp_doodle
import os
import shutil

data_dir = "../data"
template_file = "./template.html"
source_path = "../../jp_doodle/js/"
code_destination_path = "../jp_doodle_js/"
qr_source_path = "../../notebooks/quick_references/"
qr_destination_path = "../quick_references/"
github_io_prefix = "https://aaronwatters.github.io/jp_doodle"
readme_template = "../../README_template.md"
readme_out = "../../README.md"
readme_prologue = """
<!--
This file was generated by building the docs.
Please do not edit this file by hand.
Instead, edit the template and rebuild to docs.
-->
"""

readme_demo_fragment = """
<tr>
    <td>
        <a href="GITHUB_PREFIX_HERE/url_HERE">
        <img width="200px" src="docs/SCREENSHOT_HERE">
        </a>
    </td>
    <td>
        <h4><a href="GITHUB_PREFIX_HERE/url_HERE">
        TITLE_HERE
        </a></h4>
        <div>
        PROLOGUE_HERE
        </div>
    </td>
</tr>
"""

class HTML_Builder:

    def __init__(self):
        self.file_name_to_info = {}
        self.template_text = open(template_file).read()

    def parse_data_files(self):
        data_files = os.listdir(data_dir)
        for filename in data_files:
            if not filename.endswith(".txt"):
                continue
            filepath = os.path.join(data_dir, filename)
            info = {}
            key = None
            value_lines = []
            def emit():
                if key is not None and value_lines:
                    info[key] = ("".join(value_lines)).strip()
                value_lines[:] = []
            for line in open(filepath):
                if line.startswith("***"):
                    emit()
                    key = line[3:].strip()
                else:
                    value_lines.append(line)
            emit()
            url = filename.replace(".txt", ".html")
            # special case: index
            if url=="000_index.html":
                url = "index.html"
            info["url"] = url
            info["outfile"] = "../" + url
            self.file_name_to_info[filename] = info

    def build_readme(self):
        result = open(readme_template).read()
        divs = []
        #docs_dir = "docs/"
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            div = self.here_substitutions(readme_demo_fragment, info)
            div = div.replace("GITHUB_PREFIX_HERE", github_io_prefix)
            divs.append(div)
        all_demos = "<table>\n%s\n</table>" % ("\n".join(divs))
        result = result.replace("DEMOS_HERE", all_demos)
        assert "_HERE" not in result
        print ("writing " + repr(readme_out))
        out = open(readme_out, "w")
        out.write(readme_prologue)
        out.write(result)
        return result

    def here_substitutions(self, template, info):
        result = template
        suffix = "_HERE"
        for key in info:
            placeholder = key + suffix
            replacement = info[key]
            result = result.replace(placeholder, replacement)
        return result

    def old_build_menu(self):
        entries = []
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            url = info["url"]
            title = info["TITLE"]
            screenshot = info["SCREENSHOT"]
            entry = '<li><a href="%s">%s <br><img src="%s" style="background-color:#cdf" width="50px"></a></li>' % (url, title, screenshot)
            entries.append(entry)
        self.menu = "<ul>\n%s\n</ul>" % "\n".join(entries)

    def build_menu(self):
        entries = []
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            url = info["url"]
            title = info["TITLE"]
            screenshot = info["SCREENSHOT"]
            entry = (
                '<div>' +
                '<a href="%s">' +
                '<div> %s </div>' +
                '<div> <img src="%s" style="background-color:#cdf" width="50px"/> </div>' +
                '</a>' +
                '</div>'
                ) % (url, title, screenshot)
            entries.append(entry)
        cols = " 125px" * 10
        divs = "\n".join(entries)
        self.menu = '<div style="display: grid; margin: 10px; grid-template-columns: %s">\n%s\n</div>' % (cols, divs)

    def file_content(self, info):
        result = self.here_substitutions(self.template_text, info)
        result = result.replace("MENU_HERE", self.menu)
        assert "_HERE" not in result
        return result
    
    def write_files(self):
        print ("copying " + repr((source_path, code_destination_path)))
        # Copy javascript code from source area to doc area
        shutil.rmtree(code_destination_path)
        shutil.copytree(source_path, code_destination_path)
        # Copy quick refs from notebooks to doc area
        print ("copying " + repr((qr_source_path, qr_destination_path)))
        shutil.rmtree(qr_destination_path)
        shutil.copytree(qr_source_path, qr_destination_path)
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            outfile = info["outfile"]
            content = self.file_content(info)
            print("writing " + repr(outfile))
            f = open(outfile, "w")
            f.write(content)
            f.close()

if __name__ == "__main__":
    H = HTML_Builder()
    H.parse_data_files()
    H.build_menu()
    H.build_readme()
    H.write_files()

import os
import shutil

data_dir = "../data"
template_file = "./template.html"
source_path = "../../jp_doodle/js/"
code_destination_path = "../jp_doodle_js/"
qr_source_path = "../../notebooks/quick_references/"
qr_destination_path = "../quick_references/"
github_io_prefix = "https://aaronwatters.github.io/jp_doodle"
readme_template = "../../README_template.md"
readme_out = "../../README.md"
readme_prologue = """
<!--
This file was generated by building the docs.
Please do not edit this file by hand.
Instead, edit the template and rebuild to docs.
-->
"""

readme_demo_fragment = """
<tr>
    <td>
        <a href="GITHUB_PREFIX_HERE/url_HERE">
        <img width="200px" src="docs/SCREENSHOT_HERE">
        </a>
    </td>
    <td>
        <h4><a href="GITHUB_PREFIX_HERE/url_HERE">
        TITLE_HERE
        </a></h4>
        <div>
        PROLOGUE_HERE
        </div>
    </td>
</tr>
"""

class HTML_Builder:

    def __init__(self):
        self.file_name_to_info = {}
        self.template_text = open(template_file).read()

    def parse_data_files(self):
        data_files = os.listdir(data_dir)
        for filename in data_files:
            if not filename.endswith(".txt"):
                continue
            filepath = os.path.join(data_dir, filename)
            info = {}
            key = None
            value_lines = []
            def emit():
                if key is not None and value_lines:
                    info[key] = ("".join(value_lines)).strip()
                value_lines[:] = []
            for line in open(filepath):
                if line.startswith("***"):
                    emit()
                    key = line[3:].strip()
                else:
                    value_lines.append(line)
            emit()
            url = filename.replace(".txt", ".html")
            # special case: index
            if url=="000_index.html":
                url = "index.html"
            info["url"] = url
            info["outfile"] = "../" + url
            self.file_name_to_info[filename] = info

    def build_readme(self):
        result = open(readme_template).read()
        divs = []
        #docs_dir = "docs/"
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            div = self.here_substitutions(readme_demo_fragment, info)
            div = div.replace("GITHUB_PREFIX_HERE", github_io_prefix)
            divs.append(div)
        all_demos = "<table>\n%s\n</table>" % ("\n".join(divs))
        result = result.replace("DEMOS_HERE", all_demos)
        assert "_HERE" not in result
        print ("writing " + repr(readme_out))
        out = open(readme_out, "w")
        out.write(readme_prologue)
        out.write(result)
        return result

    def here_substitutions(self, template, info):
        result = template
        suffix = "_HERE"
        for key in info:
            placeholder = key + suffix
            replacement = info[key]
            result = result.replace(placeholder, replacement)
        return result

    def old_build_menu(self):
        entries = []
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            url = info["url"]
            title = info["TITLE"]
            screenshot = info["SCREENSHOT"]
            entry = '<li><a href="%s">%s <br><img src="%s" style="background-color:#cdf" width="50px"></a></li>' % (url, title, screenshot)
            entries.append(entry)
        self.menu = "<ul>\n%s\n</ul>" % "\n".join(entries)

    def build_menu(self):
        entries = []
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            url = info["url"]
            title = info["TITLE"]
            screenshot = info["SCREENSHOT"]
            entry = (
                '<div>' +
                '<a href="%s">' +
                '<div> %s </div>' +
                '<div> <img src="%s" style="background-color:#cdf" width="50px"/> </div>' +
                '</a>' +
                '</div>'
                ) % (url, title, screenshot)
            entries.append(entry)
        cols = " 125px" * 10
        divs = "\n".join(entries)
        self.menu = '<div style="display: grid; margin: 10px; grid-template-columns: %s">\n%s\n</div>' % (cols, divs)

    def file_content(self, info):
        result = self.here_substitutions(self.template_text, info)
        result = result.replace("MENU_HERE", self.menu)
        assert "_HERE" not in result
        return result
    
    def write_files(self):
        print ("copying " + repr((source_path, code_destination_path)))
        # Copy javascript code from source area to doc area
        shutil.rmtree(code_destination_path)
        shutil.copytree(source_path, code_destination_path)
        # Copy quick refs from notebooks to doc area
        print ("copying " + repr((qr_source_path, qr_destination_path)))
        shutil.rmtree(qr_destination_path)
        shutil.copytree(qr_source_path, qr_destination_path)
        for file_name in sorted(self.file_name_to_info.keys()):
            info = self.file_name_to_info[file_name]
            outfile = info["outfile"]
            content = self.file_content(info)
            print("writing " + repr(outfile))
            f = open(outfile, "w")
            f.write(content)
            f.close()

if __name__ == "__main__":
    H = HTML_Builder()
    H.parse_data_files()
    H.build_menu()
    H.build_readme()
    H.write_files()
<!-- This file was generated by building the docs. Please do not edit this file by hand. Instead, edit the template and rebuild to docs. --> <tr>     <td>         <a href="GITHUB_PREFIX_HERE/url_HERE">         <img width="200px" src="docs/SCREENSHOT_HERE">         </a>     </td>     <td>         <h4><a href="GITHUB_PREFIX_HERE/url_HERE">         TITLE_HERE         </a></h4>         <div>         PROLOGUE_HERE         </div>     </td> </tr> # special case: index #docs_dir = "docs/" #cdf" width="50px"></a></li>' % (url, title, screenshot) #cdf" width="50px"/> </div>' + # Copy javascript code from source area to doc area # Copy quick refs from notebooks to doc area