-*- mode: org -*-
#+TITLE:       sisu json
#+DESCRIPTION: documents - structuring, various output representations & search
#+FILETAGS:    :sisu:json:
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT:   Copyright (C) 2015 - 2021 Ralph Amissah
#+LANGUAGE:    en
#+STARTUP:     content hideblocks hidestars noindent entitiespretty
#+OPTIONS:     H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+PROPERTY:    header-args  :exports code
#+PROPERTY:    header-args+ :noweb yes
#+PROPERTY:    header-args+ :eval no
#+PROPERTY:    header-args+ :results no
#+PROPERTY:    header-args+ :cache no
#+PROPERTY:    header-args+ :padline no
#+PROPERTY:    header-args+ :mkdirp yes
* json.rb
#+BEGIN_SRC ruby  :tangle "../lib/sisu/json.rb"
<>
module SiSU_JSON
  require_relative 'se_hub_particulars'                 # se_hub_particulars.rb
    include SiSU_Particulars
  require_relative 'se'                                 # se.rb
    include SiSU_Env
  require_relative 'json_shared'                        # json_shared.rb
    include SiSU_JSON_Munge
  require_relative 'json_format'                        # json_format.rb
    include SiSU_JSON_Format
  require_relative 'json_persist'                       # json_persist.rb
  require_relative 'shared_metadata'                    # shared_metadata.rb
  @@alt_id_count=0
  @@tablefoot=''
  class Source
    def initialize(opt)
      @opt=opt
      @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
    end
    def read
      begin
        @env,@md,@ao_array=@particulars.env,@particulars.md,@particulars.ao_array
        unless @opt.act[:quiet][:set]==:on
          tool=if (@opt.act[:verbose_plus][:set]==:on \
          || @opt.act[:maintenance][:set]==:on)
            @env.program.web_browser +
            ' file://' +
            @md.file.output_path.json.dir + '/' +
            @md.file.base_filename.json
          elsif @opt.act[:verbose][:set]==:on
            @env.program.web_browser +
            ' file://' +
            @md.file.output_path.json.dir + '/' +
            @md.file.base_filename.json
          else "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}"
          end
          (@opt.act[:verbose][:set]==:on \
          || @opt.act[:verbose_plus][:set]==:on \
          || @opt.act[:maintenance][:set]==:on) \
          ? SiSU_Screen::Ansi.new(
              @opt.act[:color_state][:set],
              'JSON',
              tool
            ).green_hi_blue
          : SiSU_Screen::Ansi.new(
              @opt.act[:color_state][:set],
              'JSON',
              tool
            ).green_title_hi
          if (@opt.act[:verbose_plus][:set]==:on \
          || @opt.act[:maintenance][:set]==:on)
            SiSU_Screen::Ansi.new(
              @opt.act[:color_state][:set],
              @opt.fns,
                '/' + @md.file.output_path.json.dir +
                '/' + @md.file.base_filename.json
            ).flow
          end
        end
        SiSU_JSON::Source::Songsheet.new(@particulars).song
      rescue
        SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
          __LINE__.to_s + ':' + __FILE__
        end
      ensure
        SiSU_Env::CreateSite.new(@opt).cp_css
        Dir.chdir(@opt.f_pth[:pth])
      end
    end
    private
    class Songsheet
      def initialize(particulars)
        @env,@md,@ao_array,@particulars=
          particulars.env,particulars.md,particulars.ao_array,particulars
        @file=SiSU_Env::FileOp.new(@md)
      end
      def song
        begin
          SiSU_JSON::Source::Scroll.new(@particulars).songsheet
        rescue
          SiSU_Errors::Rescued.new($!,$@,@md.opt.selections.str,@md.fns).location do
            __LINE__.to_s + ':' + __FILE__
          end
        ensure
        end
      end
    end
    class Scroll
      require_relative 'json_shared'                   # json_shared.rb #check already called
      require_relative 'txt_shared'                     # txt_shared.rb
        include SiSU_TextUtils
      require_relative 'css'                            # css.rb
      def initialize(particulars)
        @env,@md,@ao_array=particulars.env,particulars.md,particulars.ao_array
        @tab="\t"
        @trans=SiSU_JSON_Munge::Trans.new(@md)
        @sys=SiSU_Env::SystemCall.new
        @per=SiSU_JSON_Persist::Persist.new
      end
      def songsheet
        begin
          pre
          @data=markup(@ao_array)
          post
          publish
        ensure
          SiSU_JSON_Persist::Persist.new.persist_init
        end
      end
    protected
      def embedded_endnotes(dob='')
        dob.obj=dob.obj.gsub(/#{Mx[:en_a_o]}(\d+)\s+(.+?)#{Mx[:en_a_c]}/,
            '\1 \2 \1 \2 \1 \2 
##{Ax[:tab]*2}\\1. \\2
##{Ax[:tab]*1}  
#WOK
#)
          @endnotes << wrap
        end
      end
      def json_head
        #metadata=SiSU_Metadata::Summary.new(@md).json.metadata
        #@per.head << metadata
      end
      def name_tags(dob)
        tags=''
        if defined? dob.tags \
        and dob.tags.length > 0 # insert tags "hypertargets"
          dob.tags.each do |t|
            tags=tags << %{\1 '). #footnote/endnote clean
            gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/,'\1 ')
          util=SiSU_JSONutils::Clean.new(dob.obj)
          wrapped=util.line_json_clean
          @per.body << Ax[:tab]*1 + '{'
          if defined? dob.ocn and dob.ocn
            @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
          end
          if lv                                                                 # main text, contents, body KEEP
            @per.body <<
              Ax[:tab]*2 + %{"object": "} + wrapped + %{ } +
              ((@endnotes.length > 0) ? '",' : '"')
          else
            @per.body <<
              Ax[:tab]*2 + '"object": "' + wrapped +
              ((@endnotes.length > 0) ? '",' : '"')
          end
          if @endnotes.length > 0                                               # main text, endnotes KEEP
            @per.body <<
              Ax[:tab]*2 + '"endnotes": "' +
              @endnotes.compact.join.strip + '"'
          end
          @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
          @endnotes=[]
        end
      end
      def block_structure(dob)
        dob=@trans.markup_block(dob)
        dob.obj=dob.obj.strip.
          gsub(/#{Mx[:en_a_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_a_c]}/,'\1 '). #footnote/endnote clean
          gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/,'\1 ') #footnote/endnote clean
        @per.body << Ax[:tab]*1 + '{'
        if defined? dob.ocn and dob.ocn
          @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
        end
        @per.body <<
          Ax[:tab]*2 + '"object": "' + dob.obj + '"'
          #((@endnotes.length > 0) ? '",' : '"')
        @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
      end
      def group_structure(dob)
        dob=@trans.markup_group(dob)
        dob.obj=dob.obj.strip.
          gsub(/#{Mx[:en_a_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_a_c]}/,'\1 '). #footnote/endnote clean
          gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/,'\1 ') #footnote/endnote clean
        @per.body << Ax[:tab]*1 + '{'
        if defined? dob.ocn and dob.ocn
          @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
        end
        @per.body <<
          Ax[:tab]*2 + '"object": "' + dob.obj + '"'
          #((@endnotes.length > 0) ? '",' : '"')
        @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
      end
      def poem_structure(dob)
        dob=@trans.markup_group(dob)
        dob.obj=dob.obj.strip
        @per.body << Ax[:tab]*1 + '{'
        if defined? dob.ocn and dob.ocn
          @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
        end
        @per.body <<
          Ax[:tab]*2 + '"object": "' + dob.obj + '"'
          #((@endnotes.length > 0) ? '",' : '"')
        @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
      end
      def code_structure(dob)
        dob=@trans.markup_group(dob)
        dob.obj=dob.obj.gsub(/\s\s/,'  ').strip
        @per.body << Ax[:tab]*1 + '{'
        if defined? dob.ocn and dob.ocn
          @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
        end
        @per.body <<
          Ax[:tab]*2 + '"object": "' + dob.obj + '"'
          #((@endnotes.length > 0) ? '",' : '"')
        @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
      end
      def table_structure(dob)
        table=SiSU_JSON_Shared::TableJSON.new(dob)
        @per.body << Ax[:tab]*1 + '{'
        if defined? dob.ocn and dob.ocn
          @per.body << Ax[:tab]*2 + '"ocn": ' + dob.ocn.to_s + '",'
        end
        @per.body <<
          Ax[:tab]*2 + '"object": "' + table.table.obj + '"'
        #((@endnotes.length > 0) ? '",' : '"')
        @per.body << Ax[:tab]*1 + '},' # unless is last object then '}'
      end
      def markup(data)
        @endnotes=[]
        @rcdc=false
        @level,@cont,@copen,@json_contents_close=[],[],[],[]
        json_head
        (0..7).each { |x| @cont[x]=@level[x]=false }
        (4..7).each { |x| @json_contents_close[x]='' }
        data.each_with_index do |dob,i|
          dob=@trans.char_enc.utf8(dob) if @sys.locale =~/utf-?8/i #% utf8
          dob=@trans.markup(dob)
          if @rcdc==false \
          and (dob.obj =~/~meta/ \
          and dob.obj =~/Document Information/)
            @rcdc=true
          end
          if dob.obj !~/(^#{Rx[:meta]}|#{Mx[:br_eof]}|#{Mx[:br_endnotes]})/
            if not @rcdc
              x=SiSU_JSON_Format::FormatTextObject.new(@md,dob)
              if dob.is==:heading
                json_structure(dob)
                dob.obj=case dob.ln
                when 0 then x.heading_body0
                when 1 then x.heading_body1
                when 2 then x.heading_body2
                when 3 then x.heading_body3
                when 4 then x.heading_body4
                when 5 then x.heading_body5
                when 6 then x.heading_body6
                when 7 then x.heading_body7
                end
              else
                if dob.is ==:verse
                  poem_structure(dob)
                elsif dob.is ==:group
                  group_structure(dob)
                elsif dob.is ==:block
                  block_structure(dob)
                elsif dob.is ==:code
                  code_structure(dob)
                elsif dob.is ==:table
                  table_structure(dob)
                elsif dob.is ==:para \
                and dob.indent.to_s =~/[1-9]/ \
                and dob.bullet_==true
                  json_structure(dob,"indent_bullet#{dob.indent}")
                elsif dob.is ==:para \
                and dob.indent.to_s =~/[1-9]/ \
                and dob.indent == dob.hang
                  json_structure(dob,"indent#{dob.indent}")
                elsif dob.is==:para \
                and dob.hang.to_s =~/[0-9]/ \
                and dob.indent != dob.hang
                  json_structure(dob,"hang#{dob.hang.to_s}_indent#{dob.indent.to_s}")
                else json_structure(dob)
                end
              end
            end
            dob.obj=dob.obj.gsub(/#{Mx[:pa_o]}:\S+#{Mx[:pa_c]}/,'') if dob.obj
          end
        end
        6.downto(4) do |x|
          y=x - 1; v=x - 3
          @per.body << "#{Ax[:tab]*5}\n#{Ax[:tab]*y}" if @level[x]==true
        end
        3.downto(1) do |x|
          y=x - 1
          @per.body << "#{Ax[:tab]*y}" if @level[x]==true
        end
      end
      def pre
        @per.head,@per.body=[],[]
        @per.open = '{'
      end
      def post
        @per.close = '}'
      end
      def publish
        content=[]
        @per.body[-1] = @per.body[-1].gsub(/,$/, '') #= Ax[:tab]*1 + '}'
        content << @per.open << @per.head << @per.body << @per.metadata
        content << @per.tail << @per.close
        content=content.flatten.compact
        Output.new(content,@md).json
      end
    end
    class Output
      def initialize(data,md)
        @data,@md=data,md
        @file=SiSU_Env::FileOp.new(@md)
      end
      def json
        SiSU_Env::FileOp.new(@md).mkdir
        filename_json=@file.write_file.json
        @data.each do |str|
          str=str.gsub(/\A\s+\Z/m,'') #str.gsub(/^\s+$/,'')
          filename_json.puts str unless str.empty?
        end
        filename_json.close
      end
    end
  end
end
__END__
#+END_SRC
* json_parts.rb
#+BEGIN_SRC ruby  :tangle "../lib/sisu/json_parts.rb"
<>
module SiSU_Parts_JSON
  require_relative 'generic_parts'                       # generic_parts.rb
  include SiSU_Parts_Generic
  def the_line_break
    '>
module SiSU_JSONutils
  require_relative 'generic_parts'                      # generic_parts.rb
  class Clean
    def initialize(para='')
      @para=para
      #@para,@n_char_max,@n_indent,@post,=para,n_char_max,n_indent,post
      #@n_char_max_extend = n_char_max
      #@n_hang=n_hang ? n_hang : @n_indent
    end
    def line_json_clean
      @para=@para.gsub(/\1 '). #reinstate
        gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/m,'\1 ').
        gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/m,'\1 ').
        gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1 ').
        gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'\1 ').
        gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'\1 ').
        gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1 ').
        gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1 ').
        gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1').
        gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'\1 ').
        gsub(/<:pb>\s*/,''). #Fix
        gsub(/<+[-~]#>+/,'')
      if dob.is !=:code
        #embeds a red-bullet image -->
        dob.obj=dob.obj.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'\1 ').
          gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'\1 ').
          gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1 ').
          gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1')
        dob.obj=dob.obj.gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,'\1 ').
        gsub(/[*!]\{(.+?)\}[*!]/,'\1 ').
        gsub(/_\{(.+?)\}_/,'\1 ').
        gsub(/-\{(.+?)\}-/,'\1').
        gsub(/#{@md.file.output_path.xml.rel_image}\/\\1 ").
        gsub(/ |#{Mx[:nbsp]}/,' ').
        gsub(/;&([^#]|(?:[^gl][^t]|[^a][^m][^p]|[^n][^b][^s][^p])[^;])/,';&\1') # pattern not to match
      wordlist=dob.obj.scan(/&[#0-9a-z]+;|\S+|\n/) #\n needed for tables, check though added 2005w17
      dob.obj=tidywords(wordlist).join(' ').strip
      dob
    end
    def clean(str)
      str=str.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
        gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;')
    end
    def markup_fictionbook(str='',is='')
      str=str.gsub(/#{Mx[:en_a_o]}([\d+*]+).+?#{Mx[:en_a_c]}/m,'[\1] ').
        gsub(/&/,'&'). #sort
        gsub(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;').
        gsub(/(^|#{Mx[:gl_c]}|\s)&\s+/,'\1& '). #sort
        gsub(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;')
      str=str.gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,'\1 ').
        gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'\1 ').
        gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1 ').
        gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'\1 ').
        gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'\1 ').
        gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1 ').
        gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1 ').
        gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1').
        gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'\1 '). # tt, kbd
        gsub(/#{Mx[:lnk_o]}\s*(\S+?\.(?:png|jpg|gif)).+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/m,'\2 ').
          gsub(/&/,'&'). #sort
          gsub(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;').
          gsub(/(^|#{Mx[:gl_c]}|\s)&\s+/,'\1& '). #sort
          gsub(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;')
        dob.obj=dob.obj.gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,'\1 ').
          gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'\1 ').
          gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1 ').
          gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'\1 ').
          gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'\1 ').
          gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1 ').
          gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1 ').
          gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1').
          gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'\1 '). # tt, kbd
          gsub(/#{Mx[:lnk_o]}\s*(\S+?)\.(png|jpg|gif).+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/m,
            %{#{Xx[:split]}:spaces0:\n:spaces1:![]() \n:spaces0:
\n:spaces0: #{Xx[:split]}}). # common image location, else use ./images
          gsub(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(.+?)#{Mx[:url_c]}/,
            '\1 ').
          gsub(/#{Mx[:url_o]}(.+?)#{Mx[:url_c]}/,
            '\1 ').
          gsub(/#{Mx[:mk_o]}:name#(\S+?)#{Mx[:mk_c]}/,'\\2 }).
        gsub(/\b(vol):\{(.+?)\}:\1\b/m, %{\\2 }).
        gsub(/\b(pub):\{(.+?)\}:\1\b/m, %{\\2 }).
        gsub(/\b(ref):\{(.+?)\}:\1\b/m, %{\\2 }).
        gsub(/\b(desc):\{(.+?)\}:\1\b/m,%{\\2 }).
        gsub(/\b(conv):\{(.+?)\}:\1\b/m,%{\\2 }).
        gsub(/\b(ct):\{(.+?)\}:\1\b/m,  %{\\2 }).
        gsub(/\b(cty):\{(.+?)\}:\1\b/m, %{\\2 }).
        gsub(/\b(org):\{(.+?)\}:\1\b/m, %{\\2 }).
        gsub(/\b(dt):\{(.+?)\}:\1\b/m,  %{\\2 }).
        gsub(/\b(n):\{(.+?)\}:\1\b/m,   %{\\2 }).
        gsub(/([a-z]+(?:[_:.][a-z]+)*)(?::\{(.+?)\}:\1)/m,'\2 ')
    end
    def xml_semantic_tags(dob)
      if @md.sem_tag
        dob.obj.gsub!(/([a-z]+(?:[_:.][a-z]+)*)(?::\{(.+?)\}:\1)/m) {|c| xml_sem_block_paired(c) }
        dob.obj.gsub!(/([a-z]+(?:[_:.][a-z]+)*)(?::\{(.+?)\}:\1)/m) {|c| xml_sem_block_paired(c) }
        dob.obj.gsub!(/([a-z]+(?:[_:.][a-z]+)*)(?::\{(.+?)\}:\1)/m) {|c| xml_sem_block_paired(c) }
        dob.obj=dob.obj.gsub(/:\{(.+?)\}:au\b/m,             %{\\1 }).
          gsub(/:\{(.+?)\}:n\b/m,              %{\\1 }).
          gsub(/:\{(.+?)\}:ti\b/m,             %{\\1 }).
          gsub(/:\{(.+?)\}:ref\b/m,            %{\\1 }).
          gsub(/:\{(.+?)\}:desc\b/m,           %{\\1 }).
          gsub(/:\{(.+?)\}:cty\b/m,            %{\\1 }).
          gsub(/:\{(.+?)\}:org\b/m,            %{\\1 }).
          gsub(/:\{(.+?)\}:([a-z]+(?:[_:.][a-z]+)*)/m,'\1 ').
          gsub(/;\{([^}]+(?![;]))\};ti\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};qt\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};ref\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};ed\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};v\b/m,     %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};desc\b/m,  %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};def\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};trans\b/m, %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};y\b/m,     %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};ab\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};pg\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};fn?\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};mn?\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};ln?\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};in\b/m,    %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};uni\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};fac\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};inst\b/m,  %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};dept\b/m,  %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};org\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};com?\b/m,  %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};cty\b/m,   %{\\1 }).
          gsub(/;\{([^}]+(?![;]))\};([a-z]+(?:[_:.][a-z]+)*)/m,'\1 ')
      end
      dob
    end
  end
end
module SiSU_XML_Tags #Format
  require_relative 'dp'                                 # dp.rb
    include SiSU_Param
  class RDF
    include SiSU_Parts_JSON
    def initialize(md='',seg_name=[],tracker=0)
      @full_title=@subtitle=@author=@subject=@description=@publisher=@contributor=@date=@date_created=@date_issued=@date_available=@date_valid=@date_modified=@type=@format=@identifier=@source=@language=@relation=@coverage=@rights=@copyright=@owner=@keywords=''
      @md=md
      @rdfurl=%{  rdf:about="http://www.jus.uio.no/lm/toc"\n}
      if defined? @md.title.full \
      and @md.title.full                          # DublinCore 1 - title
        @rdf_title=%{    dc.title="#{seg_name}#{@md.title.full}"\n}
        @full_title=%{  
WOK
     else
<
WOK
      end
    end
    def comment_xml_sax
      desc='SiSU XML, SAX type representation'
      comment_xml(desc)
    end
    def comment_xml_node
      desc='SiSU XML, Node type representation'
      comment_xml(desc)
    end
    def comment_xml_dom
      desc='SiSU XML, DOM type representation'
      comment_xml(desc)
    end
    def metatag_html #values strung together, because some empty, and resulting output (line breaks) is much better
<>
module SiSU_JSON_Format
  require_relative 'dp'                                 # dp.rb
  require_relative 'json_parts'                         # json_parts.rb
  include SiSU_Param
  class ParagraphNumber
    def initialize(md,paranum)
      @md=md
      @paranum=(paranum \
      ? (/(\d+)/m.match(paranum)[1])
      : nil)
    end
    def display
      p_num_display=if @paranum
        @paranum.gsub(/(\d+)/,
        '' +
        '  \1 ')
      else ''
      end
      p_num_display
    end
    def name
      p_num_name=@paranum.gsub(/(\d+)/,'')
      p_num_goto
    end
  end
  class HeadInformation
    include SiSU_Parts_JSON
    def initialize #dc rdf
      @full_title=@subtitle=@author=@subject=@description=@publisher=@contributor=@date=@type=@format=@identifier=@source=@language=@relation=@coverage=@rights=@copyright=@owner=@keywords=''
      @md=@@md
      # DublinCore 1 - title
      @rdfurl=%{  rdf:about="http://www.jus.uio.no/lm/toc"\n}
      if defined? @md.title.full \
      and @md.title.full                                                      # DublinCore 1 - title
        @rdf_title=%{    dc.title="#{seg_name}#{@md.title.full}"\n}
        @full_title=%{
#{@md.html_title} 
  
#{@full_title}
#{@author}
#{@subject}
#{@description}
#{@publisher}
#{@contributor}
#{@date}
#{@date_created}
#{@date_issued}
#{@date_available}
#{@date_valid}
#{@date_modified}
#{@type}
#{@format}
#{@identifier}
#{@source}
#{@language}
#{@relation}
#{@coverage}
#{@rights}
#{@copyright}
#{@owner}
#{@png.ico}
#{@txt.generator}
#{@js.head}
\n
#{@color.body}
#{@font.css_table_file}
  \\1   })
      end
    end
    def name
      %{}
    end
  end
  class FormatTextObject
    include SiSU_Parts_JSON
    attr_accessor :md,:dob,:txt,:ocn,:format,:table,:link,:linkname,:paranum,:p_num,:headname,:banner,:url
    def initialize(md,t_o)
      @md,@t_o=md,t_o
      if t_o.class.inspect =~/Object/
        @txt=if defined? t_o.obj; t_o.obj
        else nil
        end
        @ocn=if defined? t_o.ocn; t_o.ocn.to_s
        else nil
        end
        @headname=if t_o.is==:heading and defined? t_o.name; t_o.name
        else nil
        end
      else
        if @md.opt.act[:maintenance][:set]==:on
          p __FILE__ << ':' << __LINE__.to_s
          p t_o.class
          p caller
        end
      end
      if defined? @t_o.ocn
        ocn=((@t_o.ocn.to_s =~/\d+/) ? @t_o.ocn : nil)
        @p_num=ParagraphNumber.new(@md,ocn)
      end
      if @format and not @format.empty?
        if @format=~/^\d:(\S+)/ #need more reliable marker #if @format =~ /#{Rx[:lv]}/
          headname=$1 #format[/\d~(\S+)/m,1]
          @headname=if headname =~/^[a-zA-Z]/; %{ 
  #{@p_num.ocn_display}
  <#{tag} class="#{attrib}" #{@p_num.id}>#{@p_num.name}
    #{@headname}#{@txt}
  #{tag}>
}
    end
    def heading_body
      heading_normal('p','norm')
    end
    def heading_body0
      heading_normal('h1','norm')
    end
    def heading_body1
      heading_normal('h1','norm')
    end
    def heading_body2
      heading_normal('h2','norm')
    end
    def heading_body3
      heading_normal('h3','norm')
    end
    def heading_body4
      heading_normal('h4','norm')
    end
    def heading_body5
      heading_normal('h5','norm')
    end
    def heading_body6
      heading_normal('h6','norm')
    end
    def heading_body7
      heading_normal('h7','norm')
    end
    def title_header(tag,attrib)
      %{
<#{tag} class="#{attrib}">
    #{@txt}
  #{tag}>
}
    end
    def title_header1
      title_header('h1','tiny')
    end
    def title_header2
      title_header('h2','tiny')
    end
    def title_header3
      title_header('h3','tiny')
    end
    def title_header4
      ''
    end
    def dl #check :trailer
      "#{@txt}  #{@trailer}(\1) ').
          gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((i+|iv|v|vi+|ix|x|xi+)\)/,'\1(\2) ')
      when /^\s*\(?(\d|[a-z])+\)/
        @txt.gsub(/^\((\d+|[a-z])+\)/,'(\1) ').
          gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((\d+|[a-z])+\)/,'\1(\2) ')
      when /^\s*\d{1,3}\.\s/
        @txt.gsub(/^\s*(\d+\.)/,'\1 ')
      when /^\s*[A-Z]\.\s/
        @txt.gsub(/^\s*([A-Z]\.)/,'\1 ')
      else @txt
      end
    end
    def bold_para
      %{#{the_margin.txt_0}
  
    #{@txt}
  
#{the_margin.num_css}
     
#{the_table_close}}
    end
    def bold_header
      @txt=@txt.gsub(/[1-9]~(\S+)/,'
    #{@txt}
  
#{the_margin.num_css}
     
#{the_table_close}}
    end
    def toc_head_copy_at
      %{#{@txt}
\n}
    end
    def center
      %{#{@txt}
\n}
    end
    def bold
      %{#{@txt}
\n}
    end
    def center_bold
      %{#{@txt}
\n}
    end
  end
end
__END__
#+END_SRC
* json_persist.rb
#+BEGIN_SRC ruby  :tangle "../lib/sisu/json_persist.rb"
<>
module SiSU_JSON_Persist
  class Persist
    @@persist=nil
    attr_accessor :head,:toc,:body,:tail,:open,:close,:sc,:endnotes,:book_idx,:metadata
    #attr_accessor :head,:body,:tail,:open,:close,:sc
#@@odf={ body: [], head: [], toc: [],  metadata: [], tail: [], book_idx: [], endnotes: [] }
    def initialize(args=nil)
      @@persist=args=(args ? args : (@@persist || persist_init_hash_values))
      @head=args[:head]
      @toc=args[:toc]
      @body=args[:body]
      @tail=args[:tail]
      @open=args[:open]
      @close=args[:close]
      @sc=args[:sc]
      @endnotes=args[:endnotes]
      @book_idx=args[:book_idx]
      @metadata=args[:metadata]
    end
    def head
      @head
    end
    def toc
      @toc
    end
    def body
      @body
    end
    def tail
      @tail
    end
    def open
      @open
    end
    def close
      @close
    end
    def sc
      @sc
    end
    def endnotes
      @endnotes
    end
    def book_idx
      @book_idx
    end
    def metadata
      @metadata
    end
    def persist_init_hash_values
      {
        head: [],
        toc: [],
        body: [],
        tail: [],
        open: [],
        close: [],
        sc: [],
        endnotes: [],
        book_idx: [],
        metadata: [],
      }
    end
    def persist_init
      @@persist=nil
      Persist.new(persist_init_hash_values)
    end
  end
end
__END__
#+END_SRC
* document header
#+NAME: sisu_document_header
#+BEGIN_SRC text
#encoding: utf-8
=begin
- Name: SiSU
  - Description: documents, structuring, processing, publishing, search
    json
  - Author: Ralph Amissah
    
  - Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
    2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019,
    2020, 2021, Ralph Amissah,
    All Rights Reserved.
  - License: GPL 3 or later:
    SiSU, a framework for document structuring, publishing and search
    Copyright (C) Ralph Amissah
    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
    more details.
    You should have received a copy of the GNU General Public License along with
    this program. If not, see 
    
    
  - SiSU uses:
    - Standard SiSU markup syntax,
    - Standard SiSU meta-markup syntax, and the
    - Standard SiSU object citation numbering and system
  - Homepages:
    
  - Git
    
    
=end
#+END_SRC