1 # Copyright (c) 2017, 2018, 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 include TextFileProcessing.gmk 30 31 # Helper function for SetupProcessMarkdown 32 # $1: The $1 from SetupProcessMarkdown 33 # $2: The name of the current source file, relative to the dir given in $3 34 # $3: The directory of the current source file 35 define ProcessMarkdown 36 $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2)$$($1_FILE_EXT) 37 $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE)) 38 $1_$2_INPUT_FILE := $3/$2 39 $1_$2_MARKER := $$(subst /,_,$1_$2) 40 41 ifneq ($$($1_REPLACEMENTS), ) 42 $1_$2_PANDOC_INPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre.tmp 43 44 $$(eval $$(call SetupTextFileProcessing, $1_$2_PREPROCESSED, \ 45 SOURCE_FILES := $$($1_$2_INPUT_FILE), \ 46 OUTPUT_FILE := $$($1_$2_PANDOC_INPUT), \ 47 REPLACEMENTS := $$($1_REPLACEMENTS), \ 48 )) 49 else 50 $1_$2_PANDOC_INPUT := $$($1_$2_INPUT_FILE) 51 endif 52 53 ifneq ($$($1_POST_PROCESS), ) 54 $1_$2_PANDOC_OUTPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post.tmp 55 else 56 $1_$2_PANDOC_OUTPUT := $$($1_$2_OUTPUT_FILE) 57 endif 58 59 ifneq ($$($1_CSS), ) 60 ifneq ($$(findstring http:/, $$($1_CSS)), ) 61 $1_$2_CSS_OPTION := --css '$$($1_CSS)' 62 else 63 $1_$2_CSS := $$(strip $$(call RelativePath, $$($1_CSS), $$($1_$2_TARGET_DIR))) 64 $1_$2_CSS_OPTION := --css '$$($1_$2_CSS)' 65 endif 66 endif 67 68 $1_$2_OPTIONS := $$(shell $$(GREP) _pandoc-options_: $3/$2 | $$(CUT) -d : -f 2-) 69 70 ifneq ($$($1_FILTER), ) 71 $1_$2_OPTIONS += --filter $$($1_FILTER) 72 endif 73 74 $1_$2_VARDEPS := $$($1_OPTIONS) $$($1_$2_OPTIONS) $$($1_CSS) \ 75 $$($1_REPLACEMENTS) $$($1_POST_PROCESS) 76 $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \ 77 $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps) 78 79 $$($1_$2_PANDOC_OUTPUT): $$($1_$2_PANDOC_INPUT) $$($1_$2_VARDEPS_FILE) $$($1_EXTRA_DEPS) 80 $$(call LogInfo, Converting $2 to $$($1_FORMAT)) 81 $$(call MakeDir, $$(SUPPORT_OUTPUTDIR)/markdown $$(dir $$($1_$2_PANDOC_OUTPUT))) 82 $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \ 83 $$(PANDOC) $$($1_OPTIONS) -f markdown$$(PANDOC_DISABLE_EXTENSIONS_FLAG) \ 84 -t $$($1_FORMAT) --standalone \ 85 $$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS) '$$($1_$2_PANDOC_INPUT)' \ 86 -o '$$($1_$2_PANDOC_OUTPUT)') 87 ifneq ($$(findstring $$(LOG_LEVEL), debug trace),) 88 TOO_LONG_LINES=`$$(GREP) -E -e '^.{80}.+$$$$' $$<` || true ; \ 89 if [ "x$$$$TOO_LONG_LINES" != x ]; then \ 90 $$(ECHO) "Warning: Unsuitable markdown in $$<:" ; \ 91 $$(ECHO) "The following lines are longer than 80 characters:" ; \ 92 $$(GREP) -E -n -e '^.{80}.+$$$$' $$< || true ; \ 93 fi 94 endif 95 96 # If we have no post processing, PANDOC_OUTPUT is set to OUTPUT_FILE. Otherwise 97 # PANDOC_OUTPUT is a temporary file, and we must now create the real OUTPUT_FILE. 98 ifneq ($$($1_POST_PROCESS), ) 99 $$($1_$2_OUTPUT_FILE): $$($1_$2_PANDOC_OUTPUT) 100 $$(call LogInfo, Post-processing markdown file $2) 101 $$(call MakeDir, $$(SUPPORT_OUTPUTDIR)/markdown $$($1_$2_TARGET_DIR)) 102 $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post, \ 103 $$($1_POST_PROCESS) < $$($1_$2_PANDOC_OUTPUT) > $$($1_$2_OUTPUT_FILE)) 104 endif 105 106 $1 += $$($1_$2_OUTPUT_FILE) 107 endef 108 109 ################################################################################ 110 # Setup make rules for converting a markdown file to html. 111 # 112 # Parameter 1 is the name of the rule. This name is used as variable prefix, 113 # and the targets generated are listed in a variable by that name. 114 # 115 # Remaining parameters are named arguments. These include: 116 # DEST : Destination root dir 117 # FILES : List of files to copy with absolute paths, or path relative to SRC. 118 # SRC : Source root dir; if given keep input files hierarchy relative to 119 # SRC in DEST, otherwise flatten structure into DEST. 120 # FORMAT : The target format (defaults to html5) 121 # FILE_EXT : The file extension to replace .md with (defaults to .html) 122 # OPTIONS : Additional options to pandoc 123 # EXTRA_DEPS : Additional dependencies to add to each pandoc call 124 # FILTER : Optional pandoc filter command 125 # POST_PROCESS : Optional command-line to post-process generated markdown 126 # REPLACEMENTS : Text replacements to perform on input file before processing 127 # 128 SetupProcessMarkdown = $(NamedParamsMacroTemplate) 129 define SetupProcessMarkdownBody 130 ifeq ($$($1_FILES), ) 131 $$(error FILES is missing in SetupProcessMarkdown $1) 132 endif 133 134 ifeq ($$($1_DEST), ) 135 $$(error DEST is missing in SetupProcessMarkdown $1) 136 endif 137 138 # If no target format is specified, default to html5. 139 ifeq ($$($1_FORMAT), ) 140 $1_FORMAT := html5 141 endif 142 143 ifeq ($$($1_FORMAT), man) 144 # If no file extension is specified, default to '.1'. 145 ifeq ($$($1_FILE_EXT), ) 146 $1_FILE_EXT := .1 147 endif 148 else ifeq ($$($1_FORMAT), html5) 149 ifeq ($$($1_FILE_EXT), ) 150 $1_FILE_EXT := .html 151 endif 152 else ifeq ($$($1_FORMAT), html) 153 ifeq ($$($1_FILE_EXT), ) 154 $1_FILE_EXT := .html 155 endif 156 endif 157 158 # Remove any trailing slash 159 $1_DEST := $$(patsubst %/,%,$$($1_DEST)) 160 161 ifeq ($$($1_SRC), ) 162 # No SRC given, assume we're flattening all files into output dir. 163 $$(foreach f, $$($1_FILES), \ 164 $$(eval $$(call ProcessMarkdown,$1,$$(notdir $$f),$$(patsubst %/,%,$$(dir $$f)))) \ 165 ) 166 else 167 # Remove any trailing slash 168 $1_SRC := $$(patsubst %/,%,$$($1_SRC)) 169 170 $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \ 171 $$(eval $$(call ProcessMarkdown,$1,$$f,$$($1_SRC))) \ 172 ) 173 endif 174 endef