aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v5/xml_docbook5.rb
blob: be9ad80209aee0ccf504b3f2512f1246f46d3210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# encoding: utf-8
=begin

 * Name: SiSU

 * Description: a framework for document structuring, publishing and search

 * Author: Ralph Amissah

 * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 <http://www.gnu.org/licenses/>.

   If you have Internet connection, the latest version of the GPL should be
   available at these locations:
   <http://www.fsf.org/licensing/licenses/gpl.html>
   <http://www.gnu.org/licenses/gpl.html>

   <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

 * SiSU uses:
   * Standard SiSU markup syntax,
   * Standard SiSU meta-markup syntax, and the
   * Standard SiSU object citation numbering and system

 * Hompages:
   <http://www.jus.uio.no/sisu>
   <http://www.sisudoc.org>

 * Download:
   <http://www.sisudoc.org/sisu/en/SiSU/download.html>

 * Git
   <http://sources.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
   <http://sources.sisudoc.org/?p=code/sisu.git;a=blob;f=lib/sisu/v5/xml_docbook_book.rb;hb=HEAD>

 * Ralph Amissah
   <ralph@amissah.com>
   <ralph.amissah@gmail.com>

 ** Description: Docbook book XML rendition

=end
module SiSU_XML_Docbook_Book
  require_relative 'particulars'                        # particulars.rb
    include SiSU_Particulars
  require_relative 'ao'                                 # ao.rb
  require_relative 'sysenv'                             # sysenv.rb
    include SiSU_Env
  require_relative 'txt_shared'                         # txt_shared.rb
    include SiSU_TextUtils
  require_relative 'xml_shared'                         # xml_shared.rb
    include SiSU_XML_Munge
  class Source
    def initialize(opt)
      @opt=opt
      @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
    end
    def read
      begin
        @md,@ao_array=@particulars.md,@particulars.ao_array
        @env=@particulars.env
        report
        SiSU_XML_Docbook_Book::Source::Scroll.new(@ao_array,@md).songsheet
      rescue
        SiSU_Errors::Rescued.new($!,$@,@opt.cmd,@opt.fns).location do
          __LINE__.to_s + ':' + __FILE__
        end
      ensure
      end
    end
    private
    def report
      unless @opt.act[:quiet][:set]==:on
        tool=(@opt.act[:verbose][:set]==:on \
        || @opt.act[:verbose_plus][:set]==:on \
        || @opt.act[:maintenance][:set]==:on) \
        ? "#{@env.program.docbook_viewer} #{@md.file.output_path.xml_docbook_book.dir}/#{@md.file.base_filename.xml_docbook_book}"
        : "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}"
        (@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],'DocBook',tool).green_hi_blue
        : SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],'DocBook',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.xml_docbook_book.dir}/#{@md.file.base_filename.xml_docbook_book}").flow
        end
      end
    end
    class Scroll <Source
      def initialize(data='',md='')
        @data,@md=data,md
        @trans=SiSU_XML_Munge::Trans.new(@md)
        @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(md.opt)
        @env=@particulars.env
      end
      def songsheet
        @t='sisu'
        data=@data
        if @md.opt.act[:verbose_plus][:set]==:on
          structure_collapsed(data)
        end
        #head
        #extract_endnotes
        data=markup_text(data)
        structure_build_collapsed(data)
        #tail
      end
      def space
        '  '
      end
      def tags
        # collapsed -->
        def collapsed
          %w[ 0 1 2 3 4 5 ]
        end
        def docbook(lc,chlv='')
          case lc
          when 0 then 'book'
          when 1 then lc==chlv ? 'chapter' : 'section'
          when 2 then lc==chlv ? 'chapter' : 'section'
          when 3 then lc==chlv ? 'chapter' : 'section'
          when 4 then 'section'
          when 5 then 'section'
          when 6 then 'section'
          end
        end
        self
      end
      def put(line)
        #@file_docbook.puts line                           #look into and use perhaps
        puts line if @md.opt.act[:verbose_plus][:set]==:on
      end
      def head
        <<-WOK
<?xml version="1.0" encoding="utf-8"?>
<book xmlns="http://docbook.org/ns/docbook"
  xmlns:xl="http://www.w3.org/1999/xlink"
  version="5.0">
        WOK
      end
      def markup_text(data)
        data.each_with_index do |o,i|
          if o.is ==:heading || o.is ==:para
            o=@trans.markup_docbook(o) #unless o.obj==nil
          end
        end
        data
      end
      def tail
        tail=<<-WOK
</book>
        WOK
        put(tail)
      end
      def output(o,comment='')
         puts o.lc == (0..6) \
         ? "#{space*o.lc}<#{o.lc}>[#{o.ocn}] #{o.ln} #{o.obj}</#{o.lc}>#{comment}"
         : "<#{o.lc}>[#{o.ocn}] #{o.ln} #{o.obj}</#{o.lc}>#{comment}"
      end
      def structure_collapsed(data)
        puts "\ncollapsed structure, heading outline --->\n\n"
        data.each_with_index do |o,i|
          if  (o.is ==:heading || o.is ==:heading_insert)
            output(o)
          end
        end
      end
      #def chapterlevel
      #end
      def structure_build_collapsed(data)
        #output_file=@md.file.output_path.xml_docbook_book.dir + '/' + @md.file.base_filename.xml_docbook_book
        file=SiSU_Env::FileOp.new(@md)
        filename_docbook=file.write_file.xml_docbook_book
        h=0
        @chlv=chlv=0
        doc_position=:head
        filename_docbook.puts head
        data.each_with_index do |o,i|
          if (defined? o.ocn and not o.ocn.nil?)
            ocn="\n#{Dx[:ocn_o]}#{o.ocn}#{Dx[:ocn_c]}"
            id=%{ id="o#{o.ocn}" }
          else
            ocn,id='',''
          end
          if  (o.is ==:heading || o.is ==:heading_insert)
            chlv=(o.lv.to_i == 1) \
            ? @chlv=o.lc.to_i
            : 0
            @splv=o.lc
            tag_id=o.tags[0] ? %{ id="#{o.tags[0]}" } : ''
            if doc_position ==:head
               filename_docbook.puts  %{#{space*o.lc}<title#{id}>}
              doc_position=:body_and_tail
            else
              filename_docbook.puts structure_build_tag_close(o.lc,h)
              filename_docbook.puts  %{#{space*(o.lc)}<#{tags.docbook(o.lc,chlv)}#{tag_id}>
#{space*o.lc}<title#{id}>
}
            end
            filename_docbook.puts SiSU_TextUtils::Wrap.new(o.obj + ocn,80,(@splv*2+2)).line_wrap
            filename_docbook.puts %{#{space*o.lc}</title>}
            h=o.lc
          elsif (o.of ==:para or o.of ==:block)
            filename_docbook.puts "#{space*(@splv)}<para#{id}>"
            filename_docbook.puts SiSU_TextUtils::Wrap.new(o.obj + ocn,80,(@splv*2+2)).line_wrap
            filename_docbook.puts "#{space*(@splv)}</para>"
          end
        end
        filename_docbook.puts structure_build_tag_close(0,h)
        filename_docbook.close
      end
      def structure_build_tag_close(lc,h)
        x=[]
        case h
        when 0
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 1
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 2
          x << "#{space*2}</#{tags.docbook(2,@chlv)}>" if (lc <= 2)
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 3
          x << "#{space*3}</#{tags.docbook(3,@chlv)}>" if (lc <= 3)
          x << "#{space*2}</#{tags.docbook(2,@chlv)}>" if (lc <= 2)
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 4
          x << "#{space*4}</#{tags.docbook(4,@chlv)}>" if (lc <= 4)
          x << "#{space*3}</#{tags.docbook(3,@chlv)}>" if (lc <= 3)
          x << "#{space*2}</#{tags.docbook(2,@chlv)}>" if (lc <= 2)
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 5
          x << "#{space*5}</#{tags.docbook(5)}>"       if (lc <= 5)
          x << "#{space*4}</#{tags.docbook(4,@chlv)}>" if (lc <= 4)
          x << "#{space*5}</#{tags.docbook(3,@chlv)}>" if (lc <= 3)
          x << "#{space*2}</#{tags.docbook(2,@chlv)}>" if (lc <= 2)
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        when 6
          x << "#{space*6}</#{tags.docbook(6)}>"       if (lc <= 6)
          x << "#{space*5}</#{tags.docbook(5)}>"       if (lc <= 5)
          x << "#{space*4}</#{tags.docbook(4,@chlv)}>" if (lc <= 4)
          x << "#{space*3}</#{tags.docbook(3,@chlv)}>" if (lc <= 3)
          x << "#{space*2}</#{tags.docbook(2,@chlv)}>" if (lc <= 2)
          x << "#{space*1}</#{tags.docbook(1,@chlv)}>" if (lc <= 1)
          x << "#{space*0}</#{tags.docbook(0)}>"       if (lc <= 0)
        end
        x.join("\n")
      end
    end
  end
end
__END__