make/common/ProcessMarkdown.gmk
branchihse-manpages-branch
changeset 57000 03413eb7301f
parent 56977 9221e7c3bf36
child 57035 f7a879e76ab8
equal deleted inserted replaced
56977:9221e7c3bf36 57000:03413eb7301f
    24 
    24 
    25 ifeq (,$(_MAKEBASE_GMK))
    25 ifeq (,$(_MAKEBASE_GMK))
    26   $(error You must include MakeBase.gmk prior to including ProcessMarkdown.gmk)
    26   $(error You must include MakeBase.gmk prior to including ProcessMarkdown.gmk)
    27 endif
    27 endif
    28 
    28 
       
    29 include TextFileProcessing.gmk
       
    30 
    29 # Helper function for SetupProcessMarkdown
    31 # Helper function for SetupProcessMarkdown
    30 # $1: The $1 from SetupProcessMarkdown
    32 # $1: The $1 from SetupProcessMarkdown
    31 # $2: The name of the current source file, relative to $1_SRC
    33 # $2: The name of the current source file, relative to $1_SRC
    32 define ProcessMarkdown
    34 define ProcessMarkdown
    33   $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2)$$($1_FILE_EXT)
    35   $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2)$$($1_FILE_EXT)
    34   $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE))
    36   $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE))
    35   $1_$2_INPUT_FILE := $$($1_SRC)/$2
    37   $1_$2_INPUT_FILE := $$($1_SRC)/$2
    36   $1_$2_MARKER := $$(subst /,_,$1_$2)
    38   $1_$2_MARKER := $$(subst /,_,$1_$2)
    37 
    39 
    38   ifneq ($$($1_PRE_PROCESS), )
    40   ifneq ($$($1_REPLACEMENTS), )
    39     $1_$2_PANDOC_INPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre.tmp
    41     $1_$2_PANDOC_INPUT := $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre.tmp
       
    42 
       
    43     $$(eval $$(call SetupTextFileProcessing, $1_$2_PREPROCESSED, \
       
    44         SOURCE_FILES := $$($1_$2_INPUT_FILE), \
       
    45         OUTPUT_FILE := $$($1_$2_PANDOC_INPUT), \
       
    46         REPLACEMENTS := $$($1_REPLACEMENTS), \
       
    47     ))
    40   else
    48   else
    41     $1_$2_PANDOC_INPUT := $$($1_$2_INPUT_FILE)
    49     $1_$2_PANDOC_INPUT := $$($1_$2_INPUT_FILE)
    42   endif
    50   endif
    43 
    51 
    44   ifneq ($$($1_POST_PROCESS), )
    52   ifneq ($$($1_POST_PROCESS), )
    60 
    68 
    61   ifneq ($$($1_FILTER), )
    69   ifneq ($$($1_FILTER), )
    62     $1_$2_OPTIONS += --filter $$($1_FILTER)
    70     $1_$2_OPTIONS += --filter $$($1_FILTER)
    63   endif
    71   endif
    64 
    72 
    65   $1_$2_VARDEPS := $$($1_OPTIONS) $$($1_$2_OPTIONS) $$($1_CSS)
    73   $1_$2_VARDEPS := $$($1_OPTIONS) $$($1_$2_OPTIONS) $$($1_CSS) \
       
    74       $$($1_REPLACEMENTS) $$($1_POST_PROCESS)
    66   $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
    75   $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
    67       $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps)
    76       $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER).vardeps)
    68 
    77 
    69 $$($1_$2_OUTPUT_FILE): $$($1_$2_INPUT_FILE) $$($1_$2_VARDEPS_FILE) $$($1_EXTRA_DEPS)
    78 $$($1_$2_OUTPUT_FILE): $$($1_$2_PANDOC_INPUT) $$($1_$2_VARDEPS_FILE) $$($1_EXTRA_DEPS)
    70 	$$(call LogInfo, Converting $2 to $$($1_FORMAT))
    79 	$$(call LogInfo, Converting $2 to $$($1_FORMAT))
    71 	$$(call MakeDir, $$($1_$2_TARGET_DIR) $$(SUPPORT_OUTPUTDIR)/markdown)
    80 	$$(call MakeDir, $$($1_$2_TARGET_DIR) $$(SUPPORT_OUTPUTDIR)/markdown)
    72         ifneq ($$($1_PRE_PROCESS), )
       
    73 	  $$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER)_pre, \
       
    74 	      $$($1_PRE_PROCESS) < $$($1_$2_INPUT_FILE) > $$($1_$2_PANDOC_INPUT))
       
    75         endif
       
    76 	$$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \
    81 	$$(call ExecuteWithLog, $$(SUPPORT_OUTPUTDIR)/markdown/$$($1_$2_MARKER), \
    77 	    $$(PANDOC) $$($1_OPTIONS) -f markdown -t $$($1_FORMAT) --standalone \
    82 	    $$(PANDOC) $$($1_OPTIONS) -f markdown -t $$($1_FORMAT) --standalone \
    78 	    $$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS) '$$($1_$2_PANDOC_INPUT)' \
    83 	    $$($1_$2_CSS_OPTION) $$($1_$2_OPTIONS) '$$($1_$2_PANDOC_INPUT)' \
    79 	    -o '$$($1_$2_PANDOC_OUTPUT)')
    84 	    -o '$$($1_$2_PANDOC_OUTPUT)')
    80         ifneq ($$($1_POST_PROCESS), )
    85         ifneq ($$($1_POST_PROCESS), )
   107 #   FORMAT   : The target format (defaults to html5)
   112 #   FORMAT   : The target format (defaults to html5)
   108 #   FILE_EXT : The file extension to replace .md with (defaults to .html)
   113 #   FILE_EXT : The file extension to replace .md with (defaults to .html)
   109 #   OPTIONS  : Additional options to pandoc
   114 #   OPTIONS  : Additional options to pandoc
   110 #   EXTRA_DEPS : Additional dependencies to add to each pandoc call
   115 #   EXTRA_DEPS : Additional dependencies to add to each pandoc call
   111 #   FILTER   : Optional pandoc filter command
   116 #   FILTER   : Optional pandoc filter command
       
   117 #   POST_PROCESS : Optional command-line to post-process generated markdown
       
   118 #   REPLACEMENTS : Text replacements to perform on input file before processing
   112 #
   119 #
   113 SetupProcessMarkdown = $(NamedParamsMacroTemplate)
   120 SetupProcessMarkdown = $(NamedParamsMacroTemplate)
   114 define SetupProcessMarkdownBody
   121 define SetupProcessMarkdownBody
   115   ifeq ($$($1_FILES), )
   122   ifeq ($$($1_FILES), )
   116     $$(error FILES is missing in SetupProcessMarkdown $1)
   123     $$(error FILES is missing in SetupProcessMarkdown $1)
   129   ifeq ($$($1_FORMAT), )
   136   ifeq ($$($1_FORMAT), )
   130     $1_FORMAT := html5
   137     $1_FORMAT := html5
   131   endif
   138   endif
   132 
   139 
   133   ifeq ($$($1_FORMAT), man)
   140   ifeq ($$($1_FORMAT), man)
   134     # Use preprocessing to inject the current version.
       
   135    $1_PRE_PROCESS := $(SED) -e 's/@@VERSION_SHORT@@/$(VERSION_SHORT)/g'
       
   136 
       
   137     # Pandoc's default behavior is to convert `code` to \f[C], which do not
       
   138     # show up in normal man page rendering (but shows correctly when generating
       
   139     # html). The norm for man pages is to show code as bold, so this should be
       
   140     # transformed. However, even if we fix this in our pandoc filter, pandoc prior
       
   141     # to version 2.0 cannot properly produced nested formatting in man pages
       
   142     # (see https://github.com/jgm/pandoc/issues/3568).
       
   143     # As a workaround, use post-processing with sed instead.
       
   144     #
       
   145     # As of pandoc 2.3, the termination of formatting is still broken
       
   146     # (see https://github.com/jgm/pandoc/issues/4973). We need to replace
       
   147     # \f[] with \f[R].
       
   148     $1_POST_PROCESS := $(SED) -e 's/\\f\[C\]/\\f\[CB\]/g' \
       
   149         -e 's/\\f\[\]/\\f\[R\]/g'
       
   150 
       
   151     # If no file extension is specified, default to '.1'.
   141     # If no file extension is specified, default to '.1'.
   152     ifeq ($$($1_FILE_EXT), )
   142     ifeq ($$($1_FILE_EXT), )
   153       $1_FILE_EXT := .1
   143       $1_FILE_EXT := .1
   154     endif
   144     endif
   155   else ifeq ($$($1_FORMAT), html5)
   145   else ifeq ($$($1_FORMAT), html5)