1
# Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
ifeq (,$(_MAKEBASE_GMK))
26
$(error You must include MakeBase.gmk prior to including ProcessMarkdown.gmk)
29
include TextFileProcessing.gmk
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
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)
41
# Only continue if this target file hasn't been processed already. This lets
42
# the first found source file override any other with the same name.
43
ifeq ($$($1_$2_OUTPUT_FILE_PROCESSED), )
44
$1_$2_OUTPUT_FILE_PROCESSED := true
46
ifneq ($$($1_REPLACEMENTS), )
47
$1_$2_PANDOC_INPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre.tmp
49
$$(eval $$(call SetupTextFileProcessing, $1_$2_PREPROCESSED, \
50
SOURCE_FILES := $$($1_$2_INPUT_FILE), \
51
OUTPUT_FILE := $$($1_$2_PANDOC_INPUT), \
52
REPLACEMENTS := $$($1_REPLACEMENTS), \
55
$1_$2_PANDOC_INPUT := $$($1_$2_INPUT_FILE)
58
ifneq ($$($1_POST_PROCESS), )
59
$1_$2_PANDOC_OUTPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post.tmp
61
$1_$2_PANDOC_OUTPUT := $$($1_$2_OUTPUT_FILE)
65
ifneq ($$(findstring https:/, $$($1_CSS)), )
66
$1_$2_CSS_OPTION := --css '$$($1_CSS)'
68
$1_$2_CSS := $$(strip $$(call RelativePath, $$($1_CSS), $$($1_$2_TARGET_DIR)))
69
$1_$2_CSS_OPTION := --css '$$($1_$2_CSS)'
73
# This does not need to be included in VARDEPS since it's from the actual
74
# source file. Only run the shell if the recipe gets executed below.
75
$1_$2_OPTIONS_FROM_SRC = \
76
$$(shell $$(GREP) _pandoc-options_: $3/$2 | $$(CUT) -d : -f 2-)
78
ifneq ($$($1_FILTER), )
79
$1_$2_OPTIONS := --filter $$($1_FILTER)
82
$1_$2_VARDEPS := $$($1_OPTIONS) $$($1_$2_OPTIONS) $$($1_CSS) \
83
$$($1_REPLACEMENTS) $$($1_POST_PROCESS)
84
$1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
85
$$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps)
87
$$($1_$2_PANDOC_OUTPUT): $$($1_$2_PANDOC_INPUT) $$($1_$2_VARDEPS_FILE) $$($1_EXTRA_DEPS)
88
$$(call LogInfo, Converting $2 to $$($1_FORMAT))
89
$$(call MakeDir, $$(SUPPORT_OUTPUTDIR)/markdown $$(dir $$($1_$2_PANDOC_OUTPUT)))
90
$$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \
91
$$(PANDOC) $$($1_OPTIONS) -f $$(PANDOC_MARKDOWN_FLAG) \
92
-t $$($1_FORMAT) --eol=lf --standalone \
93
$$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS_FROM_SRC) $$($1_$2_OPTIONS) \
94
'$$($1_$2_PANDOC_INPUT)' -o '$$($1_$2_PANDOC_OUTPUT)')
95
ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
96
TOO_LONG_LINES=`$$(GREP) -E -e '^.{80}.+$$$$' $$<` || true ; \
97
if [ "x$$$$TOO_LONG_LINES" != x ]; then \
98
$$(ECHO) "Warning: Unsuitable markdown in $$<:" ; \
99
$$(ECHO) "The following lines are longer than 80 characters:" ; \
100
$$(GREP) -E -n -e '^.{80}.+$$$$' $$< || true ; \
104
# If we have no post processing, PANDOC_OUTPUT is set to OUTPUT_FILE. Otherwise
105
# PANDOC_OUTPUT is a temporary file, and we must now create the real OUTPUT_FILE.
106
ifneq ($$($1_POST_PROCESS), )
107
$$($1_$2_OUTPUT_FILE): $$($1_$2_PANDOC_OUTPUT)
108
$$(call LogInfo, Post-processing markdown file $2)
109
$$(call MakeDir, $$(SUPPORT_OUTPUTDIR)/markdown $$($1_$2_TARGET_DIR))
110
$$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_post, \
111
( $$($1_POST_PROCESS) $$($1_$2_PANDOC_OUTPUT) > $$($1_$2_OUTPUT_FILE) ) )
114
$1 += $$($1_$2_OUTPUT_FILE)
118
################################################################################
119
# Setup make rules for converting a markdown file to html.
121
# Parameter 1 is the name of the rule. This name is used as variable prefix,
122
# and the targets generated are listed in a variable by that name.
124
# Remaining parameters are named arguments. These include:
125
# DEST : Destination root dir
126
# FILES : List of files to copy with absolute paths, or path relative to SRC.
127
# SRC : Source root dir; if given keep input files hierarchy relative to
128
# SRC in DEST, otherwise flatten structure into DEST.
129
# FORMAT : The target format (defaults to html5)
130
# FILE_EXT : The file extension to replace .md with (defaults to .html)
131
# OPTIONS : Additional options to pandoc
132
# EXTRA_DEPS : Additional dependencies to add to each pandoc call
133
# FILTER : Optional pandoc filter command
134
# POST_PROCESS : Optional command-line to post-process generated markdown
135
# REPLACEMENTS : Text replacements to perform on input file before processing
137
SetupProcessMarkdown = $(NamedParamsMacroTemplate)
138
define SetupProcessMarkdownBody
139
ifeq ($$($1_FILES), )
140
$$(error FILES is missing in SetupProcessMarkdown $1)
144
$$(error DEST is missing in SetupProcessMarkdown $1)
147
# If no target format is specified, default to html5.
148
ifeq ($$($1_FORMAT), )
152
ifeq ($$($1_FORMAT), man)
153
# If no file extension is specified, default to '.1'.
154
ifeq ($$($1_FILE_EXT), )
157
else ifeq ($$($1_FORMAT), html5)
158
ifeq ($$($1_FILE_EXT), )
161
else ifeq ($$($1_FORMAT), html)
162
ifeq ($$($1_FILE_EXT), )
167
# Remove any trailing slash
168
$1_DEST := $$(patsubst %/,%,$$($1_DEST))
171
# No SRC given, assume we're flattening all files into output dir.
172
$$(foreach f, $$($1_FILES), \
173
$$(eval $$(call ProcessMarkdown,$1,$$(notdir $$f),$$(patsubst %/,%,$$(dir $$f)))) \
176
# Remove any trailing slash
177
$1_SRC := $$(patsubst %/,%,$$($1_SRC))
179
$$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
180
$$(eval $$(call ProcessMarkdown,$1,$$f,$$($1_SRC))) \