#!/usr/bin/env python """ ################################################################################ # # # compile_documentation # # # ################################################################################ # # # LICENCE INFORMATION # # # # This program compiles Markdown documentation to HTML documentation suitable # # for CERN Subversion repositories and CERN Git repositories. It does this # # using Pandoc and online CSS. # # # # Will Breaden Madden, w.bm@cern.ch # # # # Subject to ATLAS Data Access Policy, this software is released under the # # terms of the GNU General Public License version 3 (GPLv3). # # # # For a copy of the ATLAS Data Access Policy, see # # DOI: 10.7483/OPENDATA.ATLAS.T9YR.Y7MZ or http://opendata.cern.ch/record/413. # # # # For a copy of the GNU General Public License, see # # http://www.gnu.org/licenses/. # # # ################################################################################ Usage: program [options] Options: -h, --help display help message --version display version and exit --MarkdownFileName=FILE Markdown file [default: README.md] --HTMLSVNFileName=FILE SVN HTML file [default: README_SVN.html] --HTMLGitFileName=FILE Git HTML file [default: README.html] --compileHTMLSVN=BOOL compile HTML for SVN [default: True] --compileHTMLGit=BOOL compile HTML for Git [default: True] """ name = "compile_documentation" version = "2015-02-20T2333Z" import os import sys import subprocess import docopt import distutils.version def main(options): MarkdownFileName = options["--MarkdownFileName"] HTMLSVNFileName = options["--HTMLSVNFileName"] HTMLGitFileName = options["--HTMLGitFileName"] compileHTMLGit = options["--compileHTMLGit"] compileHTMLSVN = options["--compileHTMLSVN"] if compileHTMLGit in ["True", "true"]: compileHTMLGit = True else: compileHTMLGit = False if compileHTMLSVN in ["True", "true"]: compileHTMLSVN = True else: compileHTMLSVN = False ensure_program_available("pandoc") ensure_version_Pandoc() compile_documentation_SVN( MarkdownFileName = MarkdownFileName, HTMLSVNFileName = HTMLSVNFileName ) compile_documentation_Git( HTMLSVNFileName = HTMLSVNFileName, HTMLGitFileName = HTMLGitFileName ) def ensure_file_existence(fileName): if not os.path.isfile(os.path.expandvars(fileName)): print("file {fileName} does not exist".format( fileName = fileName )) sys.exit() def which(program): def is_exe(fpath): return(os.path.isfile(fpath) and os.access(fpath, os.X_OK)) fpath, fname = os.path.split(program) if fpath: if is_exe(program): return(program) else: for path in os.environ["PATH"].split(os.pathsep): path = path.strip('"') exe_file = os.path.join(path, program) if is_exe(exe_file): return(exe_file) return(None) def ensure_program_available(program): if which(program) is None: print("program {program} not available".format( program = program )) sys.exit() def get_version_Pandoc(): process = subprocess.Popen(["pandoc", "--version"], stdout=subprocess.PIPE) output, error = process.communicate() version = output.split("\n")[0].split(" ")[1] return(version) def ensure_version_Pandoc(): versionMinimumRequired = "1.12.2.1" versionCurrent = get_version_Pandoc() if distutils.version.LooseVersion(versionCurrent) < \ distutils.version.LooseVersion(versionMinimumRequired): print("Pandoc version >= {version} required".format( version = versionMinimumRequired )) sys.exit() def compile_documentation_SVN( MarkdownFileName = None, HTMLSVNFileName = None ): ensure_file_existence(MarkdownFileName) process = subprocess.Popen([ "pandoc", "-c", "https://cdn.rawgit.com/wdbm/style/master/SS/newswire.css", MarkdownFileName ], stdout = subprocess.PIPE) output, error = process.communicate() HTMLSVNFileContent = output with open(HTMLSVNFileName, "w") as HTMLSVNFile: HTMLSVNFile.write(HTMLSVNFileContent) def compile_documentation_Git( HTMLSVNFileName = None, HTMLGitFileName = None ): ensure_file_existence(HTMLSVNFileName) lineRemovalPatterns = [ "!DOCTYPE html", "", "", "", "" ] # Create a variable for resultant Git file content. HTMLGitFileContent = "" # Loop over the lines of the HTML SVN file, building the resultant Git file # content. If any of the line removal patterns are in a line, remove that # line. HTMLSVNFile = open(HTMLSVNFileName, "r") for line in HTMLSVNFile: if not any(pattern in line for pattern in lineRemovalPatterns): HTMLGitFileContent = HTMLGitFileContent + line # Add CSS. CSS_addition = """ """ HTMLGitFileContent = HTMLGitFileContent.replace("", CSS_addition, 1) with open(HTMLGitFileName, "w") as HTMLGitFile: HTMLGitFile.write(HTMLGitFileContent) if __name__ == "__main__": options = docopt.docopt(__doc__) if options["--version"]: print(version) exit() main(options)