Merge "Docs: Fix typo 'UIR' should be 'URI'" into oreo-dev
diff --git a/cdd_gen.sh b/cdd_gen.sh
new file mode 100755
index 0000000..c133e3f
--- /dev/null
+++ b/cdd_gen.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+#
+# Build the CDD HTML and PDF from the source files.
+# From the root directory run:
+#   ./cdd_gen.sh --version xx --branch xx
+#
+# where version is the version number and branch is the name of the AOSP branch.
+#
+# To run this script, you must install the jinja2 and wkhtmltopdf  packages
+# using the apt-get install command.
+#
+
+POSITIONAL=()
+while [[ $# -gt 0 ]]
+do
+key="$1"
+
+case $key in
+    -v|--version)
+    VERSION="$2"
+    shift # past argument
+    shift # past value
+    ;;
+    -b|--branch)
+    BRANCH="$2"
+    shift # past argument
+    shift # past value
+    ;;
+    --default)
+    DEFAULT=YES
+    shift # past argument
+    ;;
+    *)    # unknown option
+    POSITIONAL+=("$1") # save it in an array for later
+    shift # past argument
+    ;;
+esac
+done
+set -- "${POSITIONAL[@]}" # restore positional parameters
+
+echo "VERSION = ${VERSION}"
+echo "BRANCH = ${BRANCH}"
+
+current_time=$(date "+%m.%d-%H.%M")
+echo "Current Time : $current_time"
+
+filename="android-${VERSION}-cdd-${current_time}"
+echo "$filename"
+
+if [ -z "${VERSION+x}" ] || [ -z "${BRANCH+x}" ];
+then
+  echo "No variables!"
+  python make_cdd.py --output $filename;
+else
+  echo "Variables!"
+  python make_cdd.py --version $VERSION --branch $BRANCH --output $filename;
+fi
+
+wkhtmltopdf -B 1in -T 1in -L .75in -R .75in page $filename.html --footer-html source/android-cdd-footer.html /tmp/$filename-body.pdf
+wkhtmltopdf -s letter -B 0in -T 0in -L 0in -R 0in cover source/android-cdd-cover.html /tmp/$filename-cover.pdf
diff --git a/make_cdd.py b/make_cdd.py
index 36a897e..959d8ec 100755
--- a/make_cdd.py
+++ b/make_cdd.py
@@ -6,24 +6,24 @@
 python make-cdd.py --version <version number> --branch <AOSP branch>
     --output <output file name>
 
+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 argparse
-import hashlib
+import codecs
+import jinja2
 import markdown
 import os
-import pprint
 import re
-import tidylib
 import subprocess
+import tidylib
 
-# 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):
@@ -44,7 +44,7 @@
           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'))
+          html_string = markdown.markdown(codecs.open(my_path + '/' + dir + '/' + file, 'r', encoding='utf-8').read())
           child_data.append({'file': file,
                              'number': number,
                              'title': dir.split('_')[-1],
@@ -61,10 +61,11 @@
 
 
 def get_soup(section_info):
-  html_body_text = '''<!DOCTYPE html>
+  html_body_text = u'''<!DOCTYPE html>
 <head>
-<title>Android ANDROID_VERSION Compatibility Definition</title>
+<title>Android ''' + ANDROID_VERSION + ''' Compatibility Definition</title>
 <link rel="stylesheet" type="text/css" href="source/android-cdd.css"/>
+<meta charset="utf-8" />
 </head>
 <body>
 <div id="main">'''
@@ -75,6 +76,13 @@
   html_body_text +=  '</div></body><html>'
   return BeautifulSoup(html_body_text)
 
+def get_soup_devsite(section_info):
+  html_body_text = ''
+  for section in section_info:
+     for child in section['children']:
+       html_body_text += child['html']
+  return BeautifulSoup(html_body_text)
+
 
 def add_id_to_section_headers(soup):
   header_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7']
@@ -112,24 +120,15 @@
 def create_id(header_tag):
   return header_tag.contents[0].lower().replace('. ', '_').replace(' ', '_').replace('.', '_')
 
-# Utilities
-def get_immediate_subdirs(dir):
-    return [name for name in os.listdir(dir)
-            if os.path.isdir(os.path.join(dir, name))]
-
-# Odds and ends
-
-def check_section_numbering(soup):
-  header_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7']
-  for tag in header_tags:
-    headings = soup.find_all(tag)
-    header_numbers = []
-    for heading in headings:
-      header_numbers.append(re.sub(r"([\d.]*).*", r"\1"), heading.contents)
-  return true
+def decrease_headings(soup):
+  heading_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8']
+  headings =  soup.find_all(heading_tags)
+  for heading in headings:
+    level = int(re.search(r'(\d)', heading.name).groups()[0])
+    heading.name = 'h%d' % (level + 1)
+  return soup
 
 def get_version_branch_and_output():
-
   # Get command-line args.  If there aren't any, then prompt for user input.
   parser = argparse.ArgumentParser()
   parser.add_argument('--version', help='Android version')
@@ -143,12 +142,40 @@
     args.branch = raw_input('Current AOSP branch for changelog: ')
   if not args.output:
     args.output = raw_input('Base name of desired output file: ')
-
   return (args.version, args.branch, args.output)
 
-def remove_space_before_punctuation(input):
-  space_before_punc = r'\s+([.,:])'
-  return re.sub(space_before_punc, '\1')
+# Utilities
+def get_immediate_subdirs(dir):
+    return [name for name in os.listdir(dir)
+            if os.path.isdir(os.path.join(dir, name))]
+
+def render_content(page_info, template_filename):
+  fp = open(template_filename)
+  temp_file = fp.read().encode('utf8')
+  fp.close()
+  return jinja2.Template(temp_file).render(page_info)
+
+# Odds and ends
+
+def check_section_numbering(soup):
+  header_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7']
+  for tag in header_tags:
+    headings = soup.find_all(tag)
+    header_numbers = []
+    for heading in headings:
+      header_numbers.append(re.sub(r"([\d.]*).*", r"\1"), heading.contents)
+  return true
+
+# Abandoned in favor of tidy.
+def elim_para_whitespace(html):
+  new_html = re.sub(re.compile(r"(<p[^>]*>)\s*\n\s*(<a[^>]*>)\n([^<]*)\n\s*(</a>)\n\s*(</p>)", re.M),r"\1\2\3\4\5\n", html)
+  return new_html
+
+
+def elim_space_before_punc(html):
+  new_html = re.sub(re.compile(r"</a>\s+([.,;:])", re.M),r"</a>\1", html)
+  return new_html
+
 
 def main():
   # Read version and branch info and output file name.
@@ -158,24 +185,57 @@
   my_path = os.getcwd()
   section_info = get_section_info(my_path)
 
-  # Generate the HTML
+  # Get page info
+  page_info = { 'title': 'Android ANDROID_VERSION Compatibility Definition',
+                'book_path': '/_book.yaml',
+                'project_path': '/_project.yaml'
+               }
+
+  # Generate the HTML for PDF
   soup = get_soup(section_info)
   add_id_to_section_headers(soup)
   add_toc(soup)
   html = soup.prettify(formatter='html')
 
+  # Generate the HTML for devsite
+  devsite_soup = get_soup_devsite(section_info)
+  add_id_to_section_headers(devsite_soup)
+  add_id_to_section_headers(soup)
+  page_info['body_html'] =  decrease_headings(devsite_soup)
+  devsite_html = render_content(page_info, 'source/devsite_template.html')
+
+  html = soup.prettify(formatter='html')
+
   # Add version and branch info
-  html = re.sub(re.compile(r"ANDROID_VERSION"), ANDROID_VERSION, html)
-  html = re.sub(re.compile(r"CURRENT_BRANCH"), CURRENT_BRANCH, html)
+  html = re.sub(re.compile(r'ANDROID_VERSION'), ANDROID_VERSION, html)
+  html = re.sub(re.compile(r'CURRENT_BRANCH'), CURRENT_BRANCH, html)
+
+  devsite_html = re.sub(re.compile(r'ANDROID_VERSION'), ANDROID_VERSION, devsite_html)
+  devsite_html = re.sub(re.compile(r'CURRENT_BRANCH'), CURRENT_BRANCH, devsite_html)
 
   # Apply HTML Tidy to output
-  (document, errors) = tidylib.tidy_document(html, options={})
+  (document, errors) = tidylib.tidy_document(html, options={'doctype': 'omit'})
+  (devsite_document, errors) = tidylib.tidy_document(devsite_html, options={'doctype': 'omit'})
 
-  # Write output file
-  output = open('%s.html' % output_filename, "w")
-  output.write(document.encode('utf-8'))
+  # Eliminate space before punctuation
+  html = elim_space_before_punc(html)
+  devsite_html = elim_space_before_punc(devsite_html)
+
+  # Write output files
+  output = codecs.open('%s.html' % output_filename, 'w', encoding='utf-8')
+  output.write(document)
   output.close()
 
+  devsite_output = codecs.open('%s-devsite.html' % output_filename, 'w', encoding='utf-8')
+  devsite_output.write(devsite_document)
+  output.close()
+
+  # Code to generate PDF
+  # TODO(gdimino)
+
+  # 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', shell=True)
 
 if __name__ == '__main__':
   main()