Revive "Support for generating man pages from markdown." (changeset f00e76e7d53f ihse-manpages-branch
authorihse
Tue, 18 Sep 2018 14:20:24 +0200
branchihse-manpages-branch
changeset 56891 78ed9c424877
parent 56890 bedeb1e73c99
child 56892 456f67a209b8
Revive "Support for generating man pages from markdown." (changeset f00e76e7d53f in ihse-javadoc-branch in jdk9/sandbox)
make/Docs.gmk
make/common/Modules.gmk
make/common/ProcessMarkdown.gmk
--- a/make/Docs.gmk	Tue Sep 18 14:13:21 2018 +0200
+++ b/make/Docs.gmk	Tue Sep 18 14:20:24 2018 +0200
@@ -60,6 +60,9 @@
 MODULES_SOURCE_PATH := $(call PathList, $(call GetModuleSrcPath) \
     $(SUPPORT_OUTPUTDIR)/rmic/* $(TOPDIR)/src/*/share/doc/stub)
 
+# Should we enable man pages? Allow custom to overwrite.
+ENABLE_MAN_PAGES ?= true
+
 # URLs
 JAVADOC_BASE_URL := http://www.oracle.com/pls/topic/lookup?ctx=javase$(VERSION_NUMBER)&id=homepage
 BUG_SUBMIT_URL := http://bugreport.java.com/bugreport/
@@ -553,6 +556,42 @@
       ) \
     ) \
   )
+
+  ifeq ($(ENABLE_MAN_PAGES), true)
+    # We assume all man pages should reside in section 1
+
+    # Copy man pages already in troff format
+    $(foreach m, $(ALL_MODULES), \
+      $(eval MAN_$m := $(call FindModuleManDirs, $m)) \
+      $(foreach d, $(MAN_$m), \
+        $(if $(filter %.1, $(call CacheFind, $d)), \
+          $(eval $(call SetupCopyFiles, COPY_MAN_$m, \
+              SRC := $d, \
+              FILES := $(filter %.1, $(call CacheFind, $d)), \
+              DEST := $(JAVADOC_OUTPUTDIR)/man/man1, \
+          )) \
+          $(eval JDK_SPECS_TARGETS += $(COPY_MAN_$m)) \
+        ) \
+      ) \
+    )
+
+    # Generate man pages from markdown
+    $(foreach m, $(ALL_MODULES), \
+      $(eval MAN_$m := $(call FindModuleManDirs, $m)) \
+      $(foreach d, $(MAN_$m), \
+        $(if $(filter %.md, $(call CacheFind, $d)), \
+          $(eval $(call SetupProcessMarkdown, CONVERT_MARKDOWN_MAN_$m_$d, \
+              SRC := $d, \
+              FILES := $(filter %.md, $(call CacheFind, $d)), \
+              DEST := $(JAVADOC_OUTPUTDIR)/man/man1, \
+              FORMAT := man, \
+              FILE_EXT := .1, \
+          )) \
+        ) \
+        $(eval JDK_SPECS_TARGETS += $(CONVERT_MARKDOWN_MAN_$m_$d)) \
+      ) \
+    )
+  endif
 endif
 
 # Special treatment for generated documentation
--- a/make/common/Modules.gmk	Tue Sep 18 14:13:21 2018 +0200
+++ b/make/common/Modules.gmk	Tue Sep 18 14:20:24 2018 +0200
@@ -247,6 +247,8 @@
 
 SPEC_SUBDIRS += share/specs
 
+MAN_SUBDIRS += share/man
+
 # Find all module-info.java files for the current build target platform and
 # configuration.
 # Param 1 - Module to find for, set to * for finding all
@@ -305,6 +307,12 @@
     $(strip $(wildcard \
         $(foreach sub, $(SPEC_SUBDIRS), $(addsuffix /$(strip $1)/$(sub), $(TOP_SRC_DIRS)))))
 
+# Find all man dirs for a particular module
+# $1 - Module to find man dirs for
+FindModuleManDirs = \
+    $(strip $(wildcard \
+        $(foreach sub, $(MAN_SUBDIRS), $(addsuffix /$(strip $1)/$(sub), $(TOP_SRC_DIRS)))))
+
 # Construct the complete module source path
 GetModuleSrcPath = \
     $(call PathList, \
--- a/make/common/ProcessMarkdown.gmk	Tue Sep 18 14:13:21 2018 +0200
+++ b/make/common/ProcessMarkdown.gmk	Tue Sep 18 14:20:24 2018 +0200
@@ -30,8 +30,9 @@
 # $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).html
+  $1_$2_OUTPUT_FILE := $$($1_DEST)/$$(basename $2)$$($1_FILE_EXT)
   $1_$2_TARGET_DIR := $$(dir $$($1_$2_OUTPUT_FILE))
+
   ifneq ($$($1_CSS), )
     ifneq ($$(findstring http:/, $$($1_CSS)), )
       $1_$2_CSS_OPTION := --css '$$($1_CSS)'
@@ -72,11 +73,13 @@
 # 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.
-#   OPTIONS : Additional options to pandoc
+#   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 html)
+#   FILE_EXT : The file extension to replace .md with (defaults to .html)
+#   OPTIONS  : Additional options to pandoc
 #
 SetupProcessMarkdown = $(NamedParamsMacroTemplate)
 define SetupProcessMarkdownBody
@@ -93,6 +96,26 @@
     $1_SRC := $$(dir $$(firstword $$($1_FILES)))
   endif
 
+  # If no target format is specified, default to html.
+  ifeq ($$($1_FORMAT), )
+    $1_FORMAT := html
+  endif
+
+  ifeq ($$($1_FORMAT), man)
+    # 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 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'
+  endif
+
+  # If no file extension is specified, default to '.html'.
+  ifeq ($$($1_FILE_EXT), )
+    $1_FILE_EXT := .html
+  endif
+
   # Remove any trailing slash from SRC and DEST
   $1_SRC := $$(patsubst %/,%,$$($1_SRC))
   $1_DEST := $$(patsubst %/,%,$$($1_DEST))