#!/usr/bin/python2.7 # -*- coding: utf-8 -*- # Converts from the source markup format to HTML for the web version. import sys reload(sys) sys.setdefaultencoding('utf8') import glob import os import re import subprocess import sys import time from datetime import datetime import markdown import smartypants # Assumes cwd is root project dir. GREEN = '\033[32m' RED = '\033[31m' DEFAULT = '\033[0m' PINK = '\033[91m' YELLOW = '\033[33m' CHAPTERS = [ "致谢", "序", "架构,性能和游戏", "重访设计模式", "命令模式", "享元模式", "观察者模式", "原型模式", "单例模式", "状态模式", "序列模式", "双缓冲模式", "游戏循环", "更新方法", "行为模式", "字节码", "子类沙箱", "类型对象", "解耦模式", "组件模式", "事件队列", "服务定位器", "优化模式", "数据局部性", "脏标识模式", "对象池模式", "空间分区" ] CHAPTERS_HTML = [ "Acknowledgements", "Introduction", "Architecture, Performance, and Games", "Design Patterns Revisited", "Command", "Flyweight", "Observer", "Prototype", "Singleton", "State", "Sequencing Patterns", "Double Buffer", "Game Loop", "Update Method", "Behavioral Patterns", "Bytecode", "Subclass Sandbox", "Type Object", "Decoupling Patterns", "Component", "Event Queue", "Service Locator", "Optimization Patterns", "Data Locality", "Dirty Flag", "Object Pool", "Spatial Partition" ] # URLs for hyperlinks to chapters. Note that the order is significant here. # The index in this list + 1 is the chapter's number in the table of contents. CHAPTER_HREFS = [ "acknowledgements.html", "introduction.html", "architecture-performance-and-games.html", "command.html", "flyweight.html", "observer.html", "prototype.html", "singleton.html", "state.html", "double-buffer.html", "game-loop.html", "update-method.html", "bytecode.html", "subclass-sandbox.html", "type-object.html", "component.html", "event-queue.html", "service-locator.html", "data-locality.html", "dirty-flag.html", "object-pool.html", "spatial-partition.html" ] num_chapters = 0 empty_chapters = 0 total_words = 0 extension = "html" def output_path(pattern): return extension + "/" + pattern + "." + extension def cpp_path(pattern): return 'code/cpp/' + pattern + '.h' def pretty(text): '''Use nicer HTML entities and special characters.''' text = text.replace(" -- ", " — ") text = text.replace("à", "à") text = text.replace("ï", "ï") text = text.replace("ø", "ø") text = text.replace("æ", "æ") return text def format_file(path, nav, skip_up_to_date): basename = os.path.basename(path) basename = basename.split('.')[0] # See if the HTML is up to date. sourcemod = os.path.getmtime(path) sourcemod = max(sourcemod, os.path.getmtime('asset/template.html')) if os.path.exists(cpp_path(basename)): sourcemod = max(sourcemod, os.path.getmtime(cpp_path(basename))) destmod = 0 if os.path.exists(output_path(basename)): destmod = max(destmod, os.path.getmtime(output_path(basename))) if skip_up_to_date and sourcemod < destmod: return title = '' title_html = '' section = '' isoutline = False navigation = [] # Read the markdown file and preprocess it. contents = '' with open(path, 'r') as input: # Read each line, preprocessing the special codes. for line in input: stripped = line.lstrip() indentation = line[:len(line) - len(stripped)] if stripped.startswith('^'): command,_,args = stripped.rstrip('\n').lstrip('^').partition(' ') args = args.strip() if command == 'title': title = args title_html = title # Remove any discretionary hyphens from the title. title = title.replace('­', '') elif command == 'section': section = args elif command == 'code': contents = contents + include_code(basename, args, indentation) elif command == 'outline': isoutline = True else: print "UNKNOWN COMMAND:", command, args elif extension != "xml" and stripped.startswith('#'): # Build the page navigation from the headers. index = stripped.find(" ") headertype = stripped[:index] header = pretty(stripped[index:].strip()) anchor = header.lower().replace(' ', '-') anchor = anchor.translate(None, '.?!:/"') # Add an anchor to the header. contents += indentation + headertype contents += '' + header + '\n' # Build the navigation. if len(headertype) == 2: navigation.append((len(headertype), header, anchor)) else: contents += pretty(line) modified = datetime.fromtimestamp(os.path.getmtime(path)) mod_str = modified.strftime('%B %d, %Y') with open("asset/template." + extension) as f: template = f.read() # Write the output. with open(output_path(basename), 'w') as out: title_text = title section_header = "" if section != "": title_text = title + " · " + section section_href = section.lower().replace(" ", "-") section_header = '{}'.format( section_href, section) prev_link, next_link = make_prev_next(title) contents = contents.replace(')", output) def clean_up_code_xml(code): # Ditch most code formatting tags. code = re.sub(r'([^<]+)', r"\2", code) # Turn comments into something InDesign can map to a style. code = re.sub(r'([^<]+)', r"\2", code) # Turn newlines into soft returns so code blocks stay one paragraph, except # for the last one. code = code[:-1].replace("\n", "
") + "\n" return code def fix_link(match): tag = match.group(1) contents = match.group(2) href = re.search(r'href\s*=\s*"([^"]+)"', tag).group(1) # If it's not a link to a chapter, just return the contents of the link and # strip out the link itself. if not href in CHAPTER_HREFS: return contents # Turn it into a chapter number reference. return "{} ({})".format( contents, CHAPTER_HREFS.index(href) + 1) def clean_up_xhtml(html): # Replace chapter links with chapter number references and remove other # links. html = re.sub(r"]+)>([^<]+)", fix_link, html) # Ditch newlines in the middle of blocks of text. Out of sheer malice, # even though they are meaningless in actual XML, InDesign treats them # as significant. html = re.sub(r"\n(? <", "><") # Re-add newlines after closing paragraph-level tags. html = html.replace("

", "

\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") html = html.replace("", "\n") # TODO: Non-breaking spaces in ... sections. return html result = "" for chunk in chunks: if chunk == "
":
      in_code = True
      result += chunk
    elif chunk == "
": in_code = False result += chunk else: if in_code: result += clean_up_code_xml(chunk) else: result += clean_up_xhtml(chunk) return result def title_to_file(title): """Given a title like "Event Queue", converts it to the corresponding file name like "event-queue".""" return (title.lower() .replace(" ", "-") .replace(",", "")) def make_prev_next(title): """Generate the links that thread through the chapters.""" chapter_index = CHAPTERS.index(title) prev_link = "" next_link = "" if chapter_index > 0: prev_href = title_to_file(CHAPTERS_HTML[chapter_index - 1]) prev_link = '上一章'.format( prev_href, CHAPTERS_HTML[chapter_index - 1]) if chapter_index < len(CHAPTERS_HTML) - 1: next_href = title_to_file(CHAPTERS_HTML[chapter_index + 1]) next_link = '下一章'.format( next_href, CHAPTERS_HTML[chapter_index + 1]) return (prev_link, next_link) def navigation_to_html(chapter, headers): nav = '' # Section headers start two levels deep. currentdepth = 1 for depth, header, anchor in headers: if currentdepth == depth: nav += '
  • \n' while currentdepth < depth: nav += '
    • \n' currentdepth += 1 while currentdepth > depth: nav += '
    \n' currentdepth -= 1 nav += '' + header + '' # Close the lists. while currentdepth > 1: nav += '
  • \n' currentdepth -= 1 return nav def include_code(pattern, index, indentation): with open(cpp_path(pattern), 'r') as source: lines = source.readlines() code = indentation + ' :::cpp\n' inblock = False omitting = False omitting_name = False blockindent = 0 for line in lines: stripped = line.strip() if inblock: if stripped == '//^' + index: # End of our block. break elif stripped == '//^omit': omitting = not omitting elif stripped == '//^omit ' + index: # Omitting a section just for this block. Other blocks that # Contain this code may not omit it. omitting_name = not omitting_name elif stripped.startswith('//^'): # A code block comment for another block, # so just ignore it. This can occur with # nested code blocks. pass elif not omitting and not omitting_name: # Hackish. Can't strip the leading indent off of blank # lines. if stripped == '': code += '\n' else: code_line = line[blockindent:] if len(code_line) > 64: print "Warning long source line ({} chars):\n{}".format( len(code_line), code_line) code += indentation + ' ' + code_line else: if stripped == '//^' + index: inblock = True blockindent = len(line) - len(line.lstrip()) return code def buildnav(searchpath): nav = '' return nav def format_files(file_filter, skip_up_to_date): '''Process each markdown file.''' for f in glob.iglob(searchpath): if file_filter == None or file_filter in f: format_file(f, nav, skip_up_to_date) def check_sass(): sourcemod = os.path.getmtime('asset/style.scss') destmod = os.path.getmtime('html/style.css') if sourcemod < destmod: return subprocess.call(['sass', 'asset/style.scss', 'html/style.css']) print "{}✓{} style.css".format(GREEN, DEFAULT) searchpath = ('book/*.markdown') nav = buildnav(searchpath) if len(sys.argv) == 2 and sys.argv[1] == '--watch': while True: format_files(None, True) check_sass() time.sleep(0.3) else: if len(sys.argv) > 1 and sys.argv[1] == '--xml': extension = "xml" del sys.argv[1] # Can specify a file name filter to just regenerate a subset of the files. file_filter = None if len(sys.argv) > 1: file_filter = sys.argv[1] format_files(file_filter, False) """对中文统计失效。 average_word_count = total_words / (num_chapters - empty_chapters) estimated_word_count = total_words + (empty_chapters * average_word_count) percent_finished = total_words * 100 / estimated_word_count print "{}/~{} words ({}%)".format( total_words, estimated_word_count, percent_finished) """