make/common/ProcessMarkdown.gmk
author ihse
Tue, 16 Oct 2018 12:02:45 +0200
branchihse-manpages-branch
changeset 56974 8139ac807fc9
parent 56973 57ed7a70cd1f
child 56977 9221e7c3bf36
permissions -rw-r--r--
Add needed man-page preprocessing.

# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

ifeq (,$(_MAKEBASE_GMK))
  $(error You must include MakeBase.gmk prior to including ProcessMarkdown.gmk)
endif

# Helper function for SetupProcessMarkdown
# $1: The $1 from SetupProcessMarkdown
# $2: The name of the current source file, relative to $1_SRC
define ProcessMarkdown
  $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2)$$($1_FILE_EXT)
  $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE))
  $1_$2_INPUT_FILE := $$($1_SRC)/$2
  $1_$2_MARKER := $$(subst /,_,$1_$2)

  ifneq ($$($1_PRE_PROCESS), )
    $1_$2_PANDOC_INPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre.tmp
  else
    $1_$2_PANDOC_INPUT := $$($1_$2_INPUT_FILE)
  endif

  ifneq ($$($1_POST_PROCESS), )
    $1_$2_PANDOC_OUTPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post.tmp
  else
    $1_$2_PANDOC_OUTPUT := $$($1_$2_OUTPUT_FILE)
  endif

  ifneq ($$($1_CSS), )
    ifneq ($$(findstring http:/, $$($1_CSS)), )
      $1_$2_CSS_OPTION := --css '$$($1_CSS)'
    else
      $1_$2_CSS := $$(strip $$(call RelativePath, $$($1_CSS), $$($1_$2_TARGET_DIR)))
      $1_$2_CSS_OPTION := --css '$$($1_$2_CSS)'
    endif
  endif
  $1_$2_OPTIONS := $$(shell $$(GREP) _pandoc-options_: $$($1_SRC)/$2 | $$(CUT) -d : -f 2-)

  $1_$2_VARDEPS := $$($1_OPTIONS) $$($1_$2_OPTIONS) $$($1_CSS)
  $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
      $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps)

$$($1_$2_OUTPUT_FILE): $$($1_$2_INPUT_FILE) $$($1_$2_VARDEPS_FILE)
	$$(call LogInfo, Converting $2 to $$($1_FORMAT))
	$$(call MakeDir, $$($1_$2_TARGET_DIR) $$(SUPPORT_OUTPUTDIR)/markdown)
        ifneq ($$($1_PRE_PROCESS), )
	  $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre, \
	      $$($1_PRE_PROCESS) < $$($1_$2_INPUT_FILE) > $$($1_$2_PANDOC_INPUT))
        endif
	$$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \
	    $$(PANDOC) $$($1_OPTIONS) -f markdown -t $$($1_FORMAT) --standalone \
	    $$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS) '$$($1_$2_PANDOC_INPUT)' \
	    -o '$$($1_$2_PANDOC_OUTPUT)')
        ifneq ($$($1_POST_PROCESS), )
	  $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post, \
	      $$($1_POST_PROCESS) < $$($1_$2_PANDOC_OUTPUT) > $$($1_$2_OUTPUT_FILE))
        endif
        ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
	  TOO_LONG_LINES=`$$(GREP) -E -e '^.{80}.+$$$$' $$<` || true ; \
	  if [ "x$$$$TOO_LONG_LINES" != x ]; then \
	    $$(ECHO) "Warning: Unsuitable markdown in $$<:" ; \
	    $$(ECHO) "The following lines are longer than 80 characters:" ; \
	    $$(GREP) -E -n -e '^.{80}.+$$$$' $$< || true ; \
	  fi
        endif

  $1 += $$($1_$2_OUTPUT_FILE)
endef

################################################################################
# Setup make rules for converting a markdown file to html.
#
# Parameter 1 is the name of the rule. This name is used as variable prefix,
# and the targets generated are listed in a variable by that name.
#
# Remaining parameters are named arguments. These include:
#   SRC      : Source root dir (defaults to dir of first file)
#   DEST     : Dest root dir
#   FILES    : List of files to copy with absolute paths, or path relative to SRC.
#              Must be in SRC.
#   FORMAT   : The target format (defaults to html5)
#   FILE_EXT : The file extension to replace .md with (defaults to .html)
#   OPTIONS  : Additional options to pandoc
#
SetupProcessMarkdown = $(NamedParamsMacroTemplate)
define SetupProcessMarkdownBody
  ifeq ($$($1_FILES), )
    $$(error FILES is missing in SetupProcessMarkdown $1)
  endif

  ifeq ($$($1_DEST), )
    $$(error DEST is missing in SetupProcessMarkdown $1)
  endif

  # Default SRC to the dir of the first file.
  ifeq ($$($1_SRC), )
    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
  endif

  # If no target format is specified, default to html5.
  ifeq ($$($1_FORMAT), )
    $1_FORMAT := html5
  endif

  ifeq ($$($1_FORMAT), man)
    # Use preprocessing to inject the current version.
    # Also mis-use preprocessing for stuff that would be better handled as a
    # pandoc filter: raise ## headings to #, convert those headings to upper
    # case, and remove internal links.
    $1_PRE_PROCESS := $(SED) -E -e 's/@@VERSION_SHORT@@/$(VERSION_SHORT)/g' \
        -e '/^\\\#\\\# /y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
        -e 's/^\\\#\\\# /\\\# /' -e 's/(\\\#[a-z-]+)/ /g'

    # Pandoc's default behavior is to convert `code` to \f[C], which do not
    # show up in normal man page rendering (but shows correctly when generating
    # html). Normally, we could fix this by a pandoc filter, but pandoc prior
    # to version 2.0 cannot properly produced nested formatting in man pages
    # (see https://github.com/jgm/pandoc/issues/3568).
    # As a workaround, use post-processing with sed.
    $1_POST_PROCESS := $(SED) -e 's/\\f\[C\]/\\f\[CB\]/g'

    # If no file extension is specified, default to '.1'.
    ifeq ($$($1_FILE_EXT), )
      $1_FILE_EXT := .1
    endif
  else ifeq ($$($1_FORMAT), html5)
    ifeq ($$($1_FILE_EXT), )
      $1_FILE_EXT := .html
    endif
  else ifeq ($$($1_FORMAT), html)
    ifeq ($$($1_FILE_EXT), )
      $1_FILE_EXT := .html
    endif
  endif

  # Remove any trailing slash from SRC and DEST
  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
  $1_DEST := $$(patsubst %/,%,$$($1_DEST))

  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
    $$(eval $$(call ProcessMarkdown,$1,$$f)) \
  )
endef