#!/usr/bin/python """ Utility for building the CDD from component markdown files. From the compatibility/cdd directory, run python make-cdd.py. Each generated CDD file is marked with a hash based on the content of the input files. TODO(gdimino): Clean up and comment this code. """ from bs4 import BeautifulSoup import hashlib import markdown import os import pprint import re import tidylib import subprocess # TODO (gdimino): Clean up this code using templates # from jinja2 import Template HEADERS_FOR_TOC = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7'] ANDROID_VERSION = "7.0, (N)" TOC_PER_COL = 34 def get_section_info(my_path): # (_, _, filenames) = os.walk(my_path).next() section_info = []; # Get section info from every file whose name contains a number. TODO: fix # this ugly hack. # for rootdir, subdirs, files in os.walk(my_path): for dir in get_immediate_subdirs(my_path): # for dir in subdirs: if (not dir.isalpha() and dir != 'older-versions' and dir != '.git'): child_data = [] print 'dir = ' + dir for file in os.listdir(dir): if '.md' in file: if file == 'index.md': number = 0 else: number = int((file.split('_')[1])) print 'file = ' + file + ', dir = ' + dir html_string = markdown.markdown(unicode(open(my_path + '/' + dir + '/' + file, 'r').read(), 'utf-8')) child_data.append({'file': file, 'number': number, 'title': dir.split('_')[-1], 'html': html_string, 'children':[]}) child_data.sort(key=lambda child: child['number']) section_info.append({'id': dir, 'number': int(''.join((dir.split('_')[:-1])).replace("_", ".")), 'title': dir.split('_')[-1], 'html': '', 'children':child_data}) section_info.sort(key=lambda section: section['number']) return section_info def get_soup(section_info): html_body_text = '''
]*>)\s*\n\s*(]*>)\n([^<]*)\n\s*()\n\s*(
)", re.M),r"\1\2\3\4\5\n", html) return new_html def main(): my_path = os.getcwd() section_info = get_section_info(my_path) soup = get_soup(section_info) add_id_to_section_headers(soup) add_toc(soup) html = soup.prettify(formatter='html') # Add a hash to the filename, so that identidal inputs produce the same output # file. output_filename = "test-generated-cdd-%s.html" % hashlib.md5(html).hexdigest()[0:5] output = open(output_filename, "w") output.write(html.encode('utf-8')) output.close() # Code to generate PDF, needs work. # subprocess.call('wkhtmltopdf -B 1in -T 1in -L .75in -R .75in page ' + output_filename + ' --footer-html source/android-cdd-footer.html /tmp/android-cdd-body.pdf') if __name__ == '__main__': main()