From 29f1d1af6215b7be9febc2b490cee60827c9b4d8 Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 16 May 2016 16:29:08 +0200 Subject: [PATCH] Add post-processing for the sphinx htmlhelp output before build * Copies our .hhk into the build folder * Removes TOC entries that just refer to anchors - it's unnecessary and just clutters up the TOC. --- docs/Makefile | 5 +++++ docs/make.bat | 7 ++++++- docs/make.sh | 5 +++++ docs/remove_lines.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 docs/remove_lines.py diff --git a/docs/Makefile b/docs/Makefile index f6486e274..ccfcee933 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -86,6 +86,11 @@ json: .PHONY: htmlhelp htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + # Copy handwritten index file to output, overwriting auto-generated one + @cp renderdoc.hhk $(BUILDDIR)/htmlhelp + # Filter out the auto-generated TOC to remove anchor links and root index.html + @cat $(BUILDDIR)/htmlhelp/renderdoc.hhc | python remove_lines.py ".html#" | python remove_lines.py "\"index.html\"" > $(BUILDDIR)/htmlhelp/tmp + @mv $(BUILDDIR)/htmlhelp/tmp $(BUILDDIR)/htmlhelp/renderdoc.hhc @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." diff --git a/docs/make.bat b/docs/make.bat index 7fcee16ef..1289fd591 100644 --- a/docs/make.bat +++ b/docs/make.bat @@ -5,7 +5,7 @@ REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) -set BUILDDIR=../Documentation +set BUILDDIR=..\Documentation set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( @@ -117,6 +117,11 @@ if "%1" == "json" ( if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 + REM Copy handwritten index file to output, overwriting auto-generated one + copy renderdoc.hhk %BUILDDIR%\htmlhelp\ + REM Filter out the auto-generated TOC to remove anchor links and root index.html + type %BUILDDIR%\htmlhelp\renderdoc.hhc | python remove_lines.py ".html#" | python remove_lines.py "\"index.html\"" > %BUILDDIR%\htmlhelp\tmp + move %BUILDDIR%\htmlhelp\tmp %BUILDDIR%\htmlhelp\renderdoc.hhc echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. diff --git a/docs/make.sh b/docs/make.sh index 3dd0156ee..19dbebf4b 100644 --- a/docs/make.sh +++ b/docs/make.sh @@ -101,6 +101,11 @@ fi if [ $1 == "htmlhelp" ]; then $SPHINXBUILD -b htmlhelp $ALLSPHINXOPTS $BUILDDIR/htmlhelp if [ $? != 0 ]; then exit 1; fi + # Copy handwritten index file to output, overwriting auto-generated one + cp renderdoc.hhk $BUILDDIR/htmlhelp + # Filter out the auto-generated TOC to remove anchor links and root index.html + cat $BUILDDIR/htmlhelp/renderdoc.hhc | python remove_lines.py ".html#" | python remove_lines.py "\"index.html\"" > $BUILDDIR/htmlhelp/tmp + mv $BUILDDIR/htmlhelp/tmp $BUILDDIR/htmlhelp/renderdoc.hhc echo echo "Build finished; now you can run HTML Help Workshop with the " echo ".hhp project file in $BUILDDIR/htmlhelp." diff --git a/docs/remove_lines.py b/docs/remove_lines.py new file mode 100644 index 000000000..463b9a731 --- /dev/null +++ b/docs/remove_lines.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Script taken from http://stackoverflow.com/a/17579949/4070143 by inspectorG4dget +# to remove lines above and below certain string match. +# Minor modifications to read and write from stdin/stdout respectively +# and to remove some extra newlines getting added + +import sys + +def remLines(delim, above, below): + buff = [] + line = sys.stdin.readline() + while line: + if delim in line.strip(): + buff = [] + for _ in range(below): + sys.stdin.readline() + else: + if len(buff) == above: + print(buff[0].replace('\r', '').replace('\n', '')) + buff = buff[1:] + buff.append(line) + line = sys.stdin.readline() + print(''.join(buff).strip()) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Error: no search pattern specified") + else: + remLines(sys.argv[1], 2, 1)