1 # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   2 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3 #
   4 # This code is free software; you can redistribute it and/or modify it
   5 # under the terms of the GNU General Public License version 2 only, as
   6 # published by the Free Software Foundation.  Oracle designates this
   7 # particular file as subject to the "Classpath" exception as provided
   8 # by Oracle in the LICENSE file that accompanied this code.
   9 #
  10 # This code is distributed in the hope that it will be useful, but WITHOUT
  11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 # version 2 for more details (a copy is included in the LICENSE file that
  14 # accompanied this code).
  15 #
  16 # You should have received a copy of the GNU General Public License version
  17 # 2 along with this work; if not, write to the Free Software Foundation,
  18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 #
  20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 # or visit www.oracle.com if you need additional information or have any
  22 # questions.
  23 #
  24 
  25 ifeq (,$(_MAKEBASE_GMK))
  26   $(error You must include MakeBase.gmk prior to including ProcessMarkdown.gmk)
  27 endif
  28 
  29 # Helper function for SetupProcessMarkdown
  30 # $1: The $1 from SetupProcessMarkdown
  31 # $2: The name of the current source file, relative to $1_SRC
  32 define ProcessMarkdown
  33   $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2).html
  34   $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE))
  35   ifneq ($$($1_CSS), )
  36     ifneq ($$(findstring http:/, $$($1_CSS)), )
  37       $1_$2_CSS_OPTION := --css '$$($1_CSS)'
  38     else
  39       $1_$2_CSS := $$(strip $$(call RelativePath, $$($1_CSS), $$($1_$2_TARGET_DIR)))
  40       $1_$2_CSS_OPTION := --css '$$($1_$2_CSS)'
  41     endif
  42   endif
  43   $1_$2_OPTIONS = $$(shell $$(GREP) _pandoc-options_: $$($1_SRC)/$2 | $$(CUT) -d : -f 2-)
  44   $1_$2_MARKER := $$(subst /,_,$1_$2)
  45 
  46   $1_$2_VARDEPS := $$($1_OPTIONS) $$($1_CSS)
  47   $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
  48       $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps)
  49 
  50 $$($1_$2_OUTPUT_FILE): $$($1_SRC)/$2 $$($1_$2_VARDEPS_FILE)
  51         $$(call LogInfo, Converting $2 to html)
  52         $$(call MakeDir, $$($1_$2_TARGET_DIR) $$(SUPPORT_OUTPUTDIR)/markdown)
  53         $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \
  54             $$(PANDOC) $$($1_OPTIONS) -f markdown -t html5 --standalone \
  55             $$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS) '$$<' -o '$$@')
  56         ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
  57         TOO_LONG_LINES=`$$(GREP) -E -e '^.{80}.+$$$$' $$<` || true ; \
  58         if [ "x$$$$TOO_LONG_LINES" != x ]; then \
  59           $$(ECHO) "Warning: Unsuitable markdown in $$<:" ; \
  60           $$(ECHO) "The following lines are longer than 80 characters:" ; \
  61           $$(GREP) -E -n -e '^.{80}.+$$$$' $$< || true ; \
  62         fi
  63         endif
  64 
  65   $1 += $$($1_$2_OUTPUT_FILE)
  66 endef
  67 
  68 ################################################################################
  69 # Setup make rules for converting a markdown file to html.
  70 #
  71 # Parameter 1 is the name of the rule. This name is used as variable prefix,
  72 # and the targets generated are listed in a variable by that name.
  73 #
  74 # Remaining parameters are named arguments. These include:
  75 #   SRC     : Source root dir (defaults to dir of first file)
  76 #   DEST    : Dest root dir
  77 #   FILES   : List of files to copy with absolute paths, or path relative to SRC.
  78 #             Must be in SRC.
  79 #   OPTIONS : Additional options to pandoc
  80 #
  81 SetupProcessMarkdown = $(NamedParamsMacroTemplate)
  82 define SetupProcessMarkdownBody
  83   ifeq ($$($1_FILES), )
  84     $$(error FILES is missing in SetupProcessMarkdown $1)
  85   endif
  86 
  87   ifeq ($$($1_DEST), )
  88     $$(error DEST is missing in SetupProcessMarkdown $1)
  89   endif
  90 
  91   # Default SRC to the dir of the first file.
  92   ifeq ($$($1_SRC), )
  93     $1_SRC := $$(dir $$(firstword $$($1_FILES)))
  94   endif
  95 
  96   # Remove any trailing slash from SRC and DEST
  97   $1_SRC := $$(patsubst %/,%,$$($1_SRC))
  98   $1_DEST := $$(patsubst %/,%,$$($1_DEST))
  99 
 100   $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
 101     $$(eval $$(call ProcessMarkdown,$1,$$f)) \
 102   )
 103 endef