22require 'liquid'
33
44module Jekyll
5+ class SyslogNGTools
6+ class << self
7+ public
8+
9+ def write_to_file ( file_path , content )
10+ File . open ( file_path , "w" ) do |file |
11+ file . write ( content )
12+ end
13+ end
14+
15+ end # self
16+ end # SyslogNGTools
17+
518 class TooltipGen
619 class << self
720
@@ -250,12 +263,6 @@ def process_page(page)
250263 page . content = parts . join
251264 end
252265
253- def write_to_file ( file_path , content )
254- File . open ( file_path , "w" ) do |file |
255- file . write ( content )
256- end
257- end
258-
259266 def process_nav_link_items ( items , ndx , nav_links_dictionary )
260267 items . each do |item |
261268 item [ 'nav_ndx' ] = ndx
@@ -397,14 +404,66 @@ def generate_tooltips(page, write_back)
397404 #puts "\n\n\n" + page.content
398405
399406 if write_back
400- write_to_file ( page . path , page . content )
407+ SyslogNGTools . write_to_file ( page . path , page . content )
401408 end
402409
403410 puts "-----------"
404411 end # def generate_tooltips
405412
406413 end # class << self
407414 end # class TooltipGen
415+
416+ class ManpageGen
417+ class << self
418+
419+ private
420+
421+ public
422+ def generate_manpage ( page , manpages_dir )
423+ puts "collection: " + ( page . respond_to? ( :collection ) ? page . collection . label : "" ) + ", ndx: #{ page . data [ "nav_ndx" ] } , relative_path: #{ page . relative_path } "
424+
425+ FileUtils . mkdir_p ( manpages_dir )
426+
427+ page_id = page . data [ 'id' ]
428+ page_links = page . data [ "page_links" ]
429+ version_string = page . site . config [ 'product' ] [ 'version' ]
430+ link_data = page_links [ page_id ]
431+ title = link_data [ "title" ] [ 0 ] # link_data["title"] is an array of titles that all must be represented by ID already in the filtered_page_ids_sorted_by_title_len array
432+
433+ # Remove the description rendering helper part from the manpage content
434+ page_content = page . content
435+ pattern = /^.*#{ Regexp . escape ( JekyllTooltipGen_description_start_tag ) } .*$\n ?/
436+ page_content = page_content . gsub ( pattern , '' )
437+ page_content = page_content . gsub ( JekyllTooltipGen_description_end_tag , '' )
438+
439+ # Compose a manpage header for md2man like
440+ #
441+ # manname manid "11 MARCH 1969" 4.9.0 "title"
442+ # "======================================="
443+ #
444+ # "## NAME"
445+ # description
446+ #
447+ date_str = Time . now . strftime ( "%d %B %Y" )
448+ manpage_header = "#{ page . data [ "manname" ] } #{ page . data [ "manid" ] } \" #{ date_str } \" #{ version_string } \" #{ title } \" \n =======================================\n \n "
449+ if page . data [ "description" ] && false == page . data [ "description" ] . empty?
450+ manpage_header = manpage_header + "## NAME\n " + page . data [ "description" ] + "\n \n "
451+ end
452+
453+ page_content = manpage_header + page_content
454+ # TODO: Check why these are not rendered yet
455+ # Remove some special, not yet rendered liquid, markdown notations, we do not want in the manpage output either
456+ page_content = page_content . gsub ( /\{ \: [^}]*\} / , "" )
457+
458+ outfile = File . join ( manpages_dir , File . basename ( page . path ) )
459+ SyslogNGTools . write_to_file ( outfile , page_content )
460+
461+ puts "-----------"
462+ end # def generate_tooltips
463+
464+ end # class << self
465+ end # ManpageGen
466+
408467end # module jekyll
409468
410469def JekyllTooltipGen_debug_page_info ( page , details = true )
@@ -485,7 +544,7 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
485544# This is used now to
486545# - set the page nav_ndx correctly to support our custom bottom collection elements navigator
487546# - set additional page data elements that will be used during all the passes
488- # - add the description to the beginning of the page.content to get it rendred correclty the same way, together with the page content
547+ # - add the description to the beginning of the page.content to get it rendered correctly the same way, together with the page content
489548#
490549# NOTE: Do not use this site based enumeration directly for the page content manipulation as well
491550# as that needs proper per-page payload data (or TODO: figure out how to get it in that case properly)
@@ -532,10 +591,13 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
532591 end
533592end
534593
594+ JekyllManpageGen_manpages_folder = '_data/manpages'
595+
535596# 3rd pass
536597#
537598# This is used now to
538599# - render the page content manually and create the autolinks and tooltips
600+ # - create manpage input markdown files for the manual pages
539601#
540602Jekyll ::Hooks . register [ :pages , :documents ] , :pre_render do |page , payload |
541603 next if false == $JekyllTooltipGen_should_build_tooltips
@@ -555,8 +617,16 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
555617 }
556618 page . content = template . render! ( payload , info )
557619
558- Jekyll ::TooltipGen . generate_tooltips ( page , $JekyllTooltipGen_should_build_persistent_tooltips)
620+ # Generate a manpage input markdown file if manid is defined in the page front-matter
621+ if page . data [ 'manid' ]
622+ if page . data [ "manname" ] == nil
623+ puts "Error: manid found without manname in page: #{ page . relative_path } "
624+ exit 5
625+ end
626+ Jekyll ::ManpageGen . generate_manpage ( page , JekyllManpageGen_manpages_folder )
627+ end
559628
629+ Jekyll ::TooltipGen . generate_tooltips ( page , $JekyllTooltipGen_should_build_persistent_tooltips)
560630 page . content = page . content . gsub ( JekyllTooltipGen_description_start_tag , '<p id="page-description">' )
561631 page . content = page . content . gsub ( JekyllTooltipGen_description_end_tag , '</p>' )
562632end
0 commit comments