changeset 23188:e2e182a8e699

Allow custom output generators for code publishing. * scripts/general/publish.m: Seek for custon output generator named __publish_<custom format>_output__.m, if options.format is not a builtin one. Delegate knowledge about output file extension to output generators. Update docstring and describe new feature. * scripts/general/private/__publish_html_output__.m, scripts/general/private/__publish_latex_output__.m: New callback subfunction do_output_file_extension to return output file extension. Update docstring and describe new feature.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 15 Feb 2017 13:10:39 +0100
parents 9da0d6a86914
children 4a2c42792d6c
files scripts/general/private/__publish_html_output__.m scripts/general/private/__publish_latex_output__.m scripts/general/publish.m
diffstat 3 files changed, 45 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/private/__publish_html_output__.m	Tue Feb 14 21:23:17 2017 +0100
+++ b/scripts/general/private/__publish_html_output__.m	Wed Feb 15 13:10:39 2017 +0100
@@ -1,4 +1,4 @@
-## Copyright (C) 2016 Kai T. Ohlhus
+## Copyright (C) 2016-2017 Kai T. Ohlhus
 ##
 ## This file is part of Octave.
 ##
@@ -29,6 +29,8 @@
 ##
 ## @itemize @bullet
 ## @item
+## @samp{output_file_extension} ()
+## @item
 ## @samp{header} (title_str, intro_str, toc_cstr)
 ## @item
 ## @samp{footer} ()
@@ -73,6 +75,9 @@
   outstr = feval (["do_" type], varargin{:});
 endfunction
 
+function outstr = do_output_file_extension ()
+  outstr = ".html";
+endfunction
 
 function outstr = do_header (title_str, intro_str, toc_cstr)
   mathjax_str = sprintf ("%s\n",
--- a/scripts/general/private/__publish_latex_output__.m	Tue Feb 14 21:23:17 2017 +0100
+++ b/scripts/general/private/__publish_latex_output__.m	Wed Feb 15 13:10:39 2017 +0100
@@ -1,4 +1,4 @@
-## Copyright (C) 2016 Kai T. Ohlhus
+## Copyright (C) 2016-2017 Kai T. Ohlhus
 ##
 ## This file is part of Octave.
 ##
@@ -29,6 +29,8 @@
 ##
 ## @itemize @bullet
 ## @item
+## @samp{output_file_extension} ()
+## @item
 ## @samp{header} (title_str, intro_str, toc_cstr)
 ## @item
 ## @samp{footer} ()
@@ -73,6 +75,10 @@
   outstr = feval (["do_" type], varargin{:});
 endfunction
 
+function outstr = do_output_file_extension ()
+  outstr = ".tex";
+endfunction
+
 function outstr = do_header (title_str, intro_str, toc_cstr)
   publish_comment = sprintf ("%s\n",
 "",
--- a/scripts/general/publish.m	Tue Feb 14 21:23:17 2017 +0100
+++ b/scripts/general/publish.m	Wed Feb 15 13:10:39 2017 +0100
@@ -1,4 +1,4 @@
-## Copyright (C) 2016 Kai T. Ohlhus <k.ohlhus@gmail.com>
+## Copyright (C) 2016-2017 Kai T. Ohlhus <k.ohlhus@gmail.com>
 ## Copyright (C) 2010 Fotios Kasolis <fotios.kasolis@gmail.com>
 ##
 ## This file is part of Octave.
@@ -96,6 +96,18 @@
 ## supported.  To generate a @samp{doc} report, open a generated @samp{html}
 ## report with your office suite.
 ##
+## In Octave custom formats are supported by implementing all callback
+## subfunctions in a function file named
+## @samp{__publish_<custom format>_output__.m}.  To obtain a template for the
+## HTML format type:
+##
+## @example
+## @group
+## edit (fullfile (fileparts (which ("publish")), ...
+##       "private", "__publish_html_output__.m"))
+## @end group
+## @end example
+##
 ## @item
 ## @samp{outputDir} --- Full path of the directory where the generated report
 ## will be located.  If no directory is given, the report is generated in a
@@ -233,13 +245,25 @@
   if (! isfield (options, "format"))
     options.format = "html";
   else
-    options.format = validatestring (options.format, {"html", "doc", "latex",
-                                                      "ppt", "xml", "pdf"});
     ## FIXME: Implement remaining formats
-    if (any (strcmp (options.format, {"doc", "ppt", "xml"})))
+    if (any (strcmpi (options.format, {"doc", "ppt", "xml"})))
       error ('publish: Output format "%s" is not yet supported',
              options.format);
     endif
+    ## Supported or custom output format
+    supported_formats = {"html", "doc", "latex", "ppt", "xml", "pdf"};
+    if (! any (strcmpi (options.format, supported_formats)))
+      ## Check existance of custom formatter
+      custom_formatter = ["__publish_", options.format, "_output__"];
+      if (! exist (custom_formatter, "file"))
+        error (['publish: Custom output format "%s" requires the ', ...
+                'formatter function:\n\n\t%s\n\n', ...
+                '\tSee "help publish" for more information.'],
+                options.format, custom_formatter);
+      endif
+    else
+      options.format = validatestring (options.format, supported_formats);
+    endif
   endif
 
   if (! isfield (options, "outputDir"))
@@ -702,14 +726,14 @@
   ## CREATE_OUTPUT creates the desired output file
 
   formatter = [];
-  ofile_ext = "";
   switch (options.format)
     case "html"
       formatter = @__publish_html_output__;
-      ofile_ext = ".html";
     case {"latex", "pdf"}
       formatter = @__publish_latex_output__;
-      ofile_ext = ".tex";
+    otherwise
+      ## Custom formatter
+      formatter = eval (["@__publish_", options.format, "_output__"]);
   endswitch
 
   ## Use title, or if not given, the m-file name
@@ -726,7 +750,7 @@
 
   ## Write file
   [~, ofile] = fileparts (doc.m_source_file_name);
-  ofile_name = [ofile, ofile_ext];
+  ofile_name = [ofile, formatter("output_file_extension")];
   ofile = fullfile (options.outputDir, ofile_name);
   fid = fopen (ofile, "w");
   fputs (fid, content);