aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v6/ao_expand_insertions.rb
blob: 65386c48af9ab4323e581427d9eeee555000b916 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# 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/v6/ao_expand_insertions.rb;hb=HEAD>

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

 ** Description: system environment, resource control and configuration details

=end
module SiSU_AO_Insertions
  class Insertions
    def initialize(md,data)
      @md,@data=md,data
    end
    def output_filetypes_in_cmd(cmd_shortcut,lnk=nil) #make list of file types in shortcut command (as configured), e.g. when sisu -3 is used
      cf_defaults=SiSU_Env::InfoProcessingFlag.new
      cmd_list=case cmd_shortcut.inspect
      when /0/ then cf_defaults.cf_0
      when /1/ then cf_defaults.cf_1
      when /2/ then cf_defaults.cf_2
      when /3/ then cf_defaults.cf_3
      when /4/ then cf_defaults.cf_4
      when /5/ then cf_defaults.cf_5
      end
      file_type_names={}
      file_type_names[:gen],file_type_names[:src]=[],[]
      file_type_names[:gen] <<= if cmd_list =~ /y/ then "~^ { document manifest }#{lnk[:manifest]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /h/ then [" { html, segmented text }#{lnk[:html_toc]}"," { html, scroll, document in one }#{lnk[:html_doc]}"]
      end
      file_type_names[:gen] <<= if cmd_list =~ /e/ then [" { epub }#{lnk[:epub]}"]
      end
      file_type_names[:gen] <<= if cmd_list =~ /p/ then [" { pdf, landscape }#{lnk[:pdf_landscape]}"," { pdf, portrait }#{lnk[:pdf_portrait]}"]
      end
      file_type_names[:gen] <<= if cmd_list =~ /o/ then " { odf:odt, open document text }#{lnk[:odt]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /b/ then " { xhtml scroll }#{lnk[:xhtml]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /x/ then " { xml, sax }#{lnk[:xml_sax]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /X/ then " { xml, dom }#{lnk[:xml_dom]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /a/ then " { plain text utf-8 }#{lnk[:txt]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /g/ then 'wiki.txt'
      end
      file_type_names[:gen] <<= if cmd_list =~ /w/ then " { concordance }#{lnk[:html_concordance]}"
      end
      file_type_names[:gen] <<= if cmd_list =~ /N/ then " { dcc, document content certificate (digests) }#{lnk[:digest]}"
      end
      file_type_names[:src] <<= if source and cmd_shortcut =~ /s/ then " { markup source text }#{lnk[:source]}"
      end
      file_type_names[:src] <<= if cmd_shortcut =~ /S/ then " { markup source (zipped) pod }#{lnk[:sisupod]}"
      end
      file_type_names[:gen]=file_type_names[:gen].flatten
      file_type_names[:src]=file_type_names[:src].flatten
      file_type_names
    end
    def by_language(linked_doc,lng,src=nil)
      @linked_doc,@lng,@src=linked_doc,lng,src
      @base_path="#{@md.file.output_path.base.url}/#{lng}"
      def fnh
        {
          fn: @linked_doc,
        }
      end
      def path_and_file(fn,pth)
        "#{@base_path}/#{pth}/#{fn}"
      end
      def manifest
        fn=@md.file.base_filename.manifest(fnh)
        path_and_file(fn,'manifest')
      end
      def html_toc
        fn=@md.file.base_filename.html_segtoc(fnh)
        "#{@base_path}/html/#{@linked_doc}/#{fn}"
      end
      def html_doc
        fn=@md.file.base_filename.html_scroll(fnh)
        path_and_file(fn,'html')
      end
      def html_concordance
        fn=@md.file.base_filename.html_concordance
        "#{@base_path}/html/#{@linked_doc}/#{fn}"
      end
      def epub
        fn=@md.file.base_filename.epub(fnh)
        path_and_file(fn,'epub')
      end
      def pdf_landscape
        fn=@md.file.base_filename.pdf_l_a4(fnh)
        path_and_file(fn,'pdf')
      end
      def pdf_portrait
        fn=@md.file.base_filename.pdf_p_a4(fnh)
        path_and_file(fn,'pdf')
      end
      def odt
        fn=@md.file.base_filename.odt(fnh)
        path_and_file(fn,'odt')
      end
      def xhtml
        fn=@md.file.base_filename.xhtml(fnh)
        path_and_file(fn,'xhtml')
      end
      def xml_sax
        fn=@md.file.base_filename.xml_sax(fnh)
        path_and_file(fn,'xml_sax')
      end
      def xml_dom
        fn=@md.file.base_filename.xml_dom(fnh)
        path_and_file(fn,'xml_dom')
      end
      def txt
        fn=@md.file.base_filename.txt(fnh)
        path_and_file(fn,'txt')
      end
      def digest
        fn=@md.file.base_filename.hash_digest(fnh)
        path_and_file(fn,'digest')
      end
      def source
        "#{@base_path}/src/#{@src}"
      end
      def sisupod
        "#{@base_path}/src/#{@src}.zip"
      end
      self
    end
    def by_filetype(linked_doc,lng,src=nil)
      @linked_doc,@lng,@src=linked_doc,lng,src
      @lc=SiSU_Env::FilenameLanguageCodeInsert.new(@md.opt,lng).language_code_insert
      @base_path="#{@md.file.output_path.base.url}"
      def fnh
        {
          fn: @linked_doc,
          lng: @lc,
        }
      end
      def path_and_file(fn,pth)
        "#{@base_path}/#{pth}/#{fn}"
      end
      def manifest
        fn=@md.file.base_filename.manifest(fnh)
        path_and_file(fn,'manifest')
      end
      def html_toc
        fn=@md.file.base_filename.html_segtoc(fnh)
        path_and_file(fn,'html')
      end
      def html_doc
        fn=@md.file.base_filename.html_scroll(fnh)
        path_and_file(fn,'html')
      end
      def html_concordance
        fn=@md.file.base_filename.html_concordance
        path_and_file(fn,'html')
      end
      def epub
        fn=@md.file.base_filename.epub(fnh)
        path_and_file(fn,'epub')
      end
      def pdf_landscape
        fn=@md.file.base_filename.pdf_l_a4(fnh)
        path_and_file(fn,'pdf')
      end
      def pdf_portrait
        fn=@md.file.base_filename.pdf_p_a4(fnh)
        path_and_file(fn,'pdf')
      end
      def odt
        fn=@md.file.base_filename.odt(fnh)
        path_and_file(fn,'odt')
      end
      def xhtml
        fn=@md.file.base_filename.xhtml(fnh)
        path_and_file(fn,'xhtml')
      end
      def xml_sax
        fn=@md.file.base_filename.xml_sax(fnh)
        path_and_file(fn,'xml_sax')
      end
      def xml_dom
        fn=@md.file.base_filename.xml_dom(fnh)
        path_and_file(fn,'xml_dom')
      end
      def txt
        fn=@md.file.base_filename.txt(fnh)
        path_and_file(fn,'txt')
      end
      def digest
        fn=@md.file.base_filename.hash_digest(fnh)
        path_and_file(fn,'digest')
      end
      def source
        "#{@base_path}/src/#{@src}"
      end
      def sisupod
        "#{@base_path}/src/#{@src}.zip"
      end
      self
    end
    def by_filename(linked_doc,lng,src=nil)
      @linked_doc,@lng,@src=linked_doc,lng,src
      @lc=SiSU_Env::FilenameLanguageCodeInsert.new(@md.opt,lng).language_code_insert
      @base_path="#{@md.file.output_path.base.url}/#{@linked_doc}"
      def fnh
        {
          fn: @linked_doc,
          lng: @lc,
        }
      end
      def path_and_file(fn,pth=nil)
        (pth.nil?) \
        ? "#{@base_path}/#{fn}"
        : "#{@base_path}/#{pth}/#{fn}"
      end
      def manifest
        fn=@md.file.base_filename.manifest(fnh)
        path_and_file(fn)
      end
      def html_toc
        fn=@md.file.base_filename.html_segtoc(fnh)
        path_and_file(fn)
      end
      def html_doc
        fn=@md.file.base_filename.html_scroll(fnh)
        path_and_file(fn)
      end
      def html_concordance
        fn=@md.file.base_filename.html_concordance
        path_and_file(fn)
      end
      def epub
        fn=@md.file.base_filename.epub(fnh)
        path_and_file(fn,'epub')
      end
      def pdf_landscape
        fn=@md.file.base_filename.pdf_l_a4(fnh)
        path_and_file(fn)
      end
      def pdf_portrait
        fn=@md.file.base_filename.pdf_p_a4(fnh)
        path_and_file(fn)
      end
      def odt
        fn=@md.file.base_filename.odt(fnh)
        path_and_file(fn)
      end
      def xhtml
        fn=@md.file.base_filename.xhtml(fnh)
        path_and_file(fn)
      end
      def xml_sax
        fn=@md.file.base_filename.xml_sax(fnh)
        path_and_file(fn)
      end
      def xml_dom
        fn=@md.file.base_filename.xml_dom(fnh)
        path_and_file(fn)
      end
      def txt
        fn=@md.file.base_filename.txt(fnh)
        path_and_file(fn)
      end
      def digest
        fn=@md.file.base_filename.hash_digest(fnh)
        path_and_file(fn)
      end
      def source
        "#{@base_path}/#{@src}"
      end
      def sisupod
        "#{@base_path}/#{@src}.zip"
      end
      self
    end
    def expand_insertions?
      data=@data
      tuned_file,tuned_file_tmp=[],[]
      codeblock_=false
      data.each do |para|
        codeblock_=if para =~/^(?:code\{|[`]{3}\s+code)/
          true
        elsif para =~/^(?:\}code|[`]{3}(?:\s|$))/m
          false
        else codeblock_
        end
        if para !~/^%+\s/ \
        and not codeblock_ \
        and para =~/\{(?:~\^\s+)?(.+?)\s\[(?:\d(?:[sS]*))\]\}(?:\.\.\/\S+?\/|\S+?\.ss[tm]\b)/
          manifest=nil
          @u=SiSU_Env::InfoEnv.new.url
          m_cmd=''
          if defined? @u.remote
            if /(?<m_pre>.+?)\{(?<m_txt>.+?)\s\[(?<m_cmd>\d[sS]*)\]\}(?<m_source>(?<m_linked_doc>\S+?)\.ss[tm]\b)(?<m_note>.*)/m =~ para
              m_pre=m_pre.strip
            elsif /\{(?<m_txt>.+?)\s\[(?<m_cmd>\d[sS]*)\]\}(?<m_source>(?<m_linked_doc>\S+?)\.ss[tm]\b)(?<m_note>.*)/m =~ para
            end
            if m_linked_doc =~ /(\S+?)\/(\S+)/
              m_linked_doc,m_linked_doc_lang=$1,$2
            else
              m_linked_doc,m_linked_doc_lang=m_linked_doc,@md.opt.lng_base
            end
          else
            puts "error, does currently support relative paths (reltive paths were removed, as had problems for citation, and was not suited to all output types should possibly reconsider) #{__FILE__} #{__LINE__}"
            if /\{(?:~\^\s+)?(?<m_txt>.+?)\s\[(?<m_cmd>\d[sS]*)\]\}\.\.\/(?<m_linked_doc>\S+?)\/(?<m_note>\s+#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]})?/ =~ para
            end
          end
          lnk=case @md.opt.dir_structure_by
          when :language
            {
              manifest:         by_language(m_linked_doc,m_linked_doc_lang).manifest,
              html_toc:         by_language(m_linked_doc,m_linked_doc_lang).html_toc,
              html_doc:         by_language(m_linked_doc,m_linked_doc_lang).html_doc,
              epub:             by_language(m_linked_doc,m_linked_doc_lang).epub,
              pdf_landscape:    by_language(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              pdf_portrait:     by_language(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              odt:              by_language(m_linked_doc,m_linked_doc_lang).odt,
              xhtml:            by_language(m_linked_doc,m_linked_doc_lang).xhtml,
              xml_sax:          by_language(m_linked_doc,m_linked_doc_lang).xml_sax,
              xml_dom:          by_language(m_linked_doc,m_linked_doc_lang).xml_dom,
              txt:              by_language(m_linked_doc,m_linked_doc_lang).txt,
              html_concordance: by_language(m_linked_doc,m_linked_doc_lang).html_concordance,
              digest:           by_language(m_linked_doc,m_linked_doc_lang).digest,
              sisupod:          by_language(m_linked_doc,m_linked_doc_lang,m_source).sisupod,
              source:           by_language(m_linked_doc,m_linked_doc_lang,m_source).source,
            }
          when :filetype
            {
              manifest:         by_filetype(m_linked_doc,m_linked_doc_lang).manifest,
              html_toc:         by_filetype(m_linked_doc,m_linked_doc_lang).html_toc,
              html_doc:         by_filetype(m_linked_doc,m_linked_doc_lang).html_doc,
              epub:             by_filetype(m_linked_doc,m_linked_doc_lang).epub,
              pdf_landscape:    by_filetype(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              pdf_portrait:     by_filetype(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              odt:              by_filetype(m_linked_doc,m_linked_doc_lang).odt,
              xhtml:            by_filetype(m_linked_doc,m_linked_doc_lang).xhtml,
              xml_sax:          by_filetype(m_linked_doc,m_linked_doc_lang).xml_sax,
              xml_dom:          by_filetype(m_linked_doc,m_linked_doc_lang).xml_dom,
              txt:              by_filetype(m_linked_doc,m_linked_doc_lang).txt,
              html_concordance: by_filetype(m_linked_doc,m_linked_doc_lang).html_concordance,
              digest:           by_filetype(m_linked_doc,m_linked_doc_lang).digest,
              sisupod:          by_filetype(m_linked_doc,m_linked_doc_lang,m_source).sisupod,
              source:           by_filetype(m_linked_doc,m_linked_doc_lang,m_source).source,
            }
          else
            {
              manifest:         by_filename(m_linked_doc,m_linked_doc_lang).manifest,
              html_toc:         by_filename(m_linked_doc,m_linked_doc_lang).html_toc,
              html_doc:         by_filename(m_linked_doc,m_linked_doc_lang).html_doc,
              epub:             by_filename(m_linked_doc,m_linked_doc_lang).epub,
              pdf_landscape:    by_filename(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              pdf_portrait:     by_filename(m_linked_doc,m_linked_doc_lang).pdf_landscape,
              odt:              by_filename(m_linked_doc,m_linked_doc_lang).odt,
              xhtml:            by_filename(m_linked_doc,m_linked_doc_lang).xhtml,
              xml_sax:          by_filename(m_linked_doc,m_linked_doc_lang).xml_sax,
              xml_dom:          by_filename(m_linked_doc,m_linked_doc_lang).xml_dom,
              txt:              by_filename(m_linked_doc,m_linked_doc_lang).txt,
              html_concordance: by_filename(m_linked_doc,m_linked_doc_lang).html_concordance,
              digest:           by_filename(m_linked_doc,m_linked_doc_lang).digest,
              sisupod:          by_filename(m_linked_doc,m_linked_doc_lang,m_source).sisupod,
              source:           by_filename(m_linked_doc,m_linked_doc_lang,m_source).source,
            }
          end
          linked_title="#{m_pre}{#{m_txt} }#{lnk[:manifest]}#{m_note}\n\n"
          tuned_file_tmp << linked_title
          output_filetypes=output_filetypes_in_cmd(m_cmd,lnk)
          output_filetypes[:gen].each do |o_f|
            describe = o_f
            if describe
              tuned_file_tmp << if @u.remote #to double space <:br> at beginning of entry
                "#{Mx[:nbsp]*4} #{describe} "
              else # remove ...
                "[provide document placement host location]"
              end
            end
          end
          output_filetypes[:src].each do |o_f|
            describe = o_f
            if describe
              tuned_file_tmp << if @u.remote
                "#{Mx[:nbsp]*4} #{describe} "
              else
                "[provide document placement host location]"
              end
            end
          end
          tuned_file << 'group{' << tuned_file_tmp.join("\n") << '}group'
          tuned_file_tmp=[]
        else tuned_file << para
        end
      end
      tuned_file
    end
  end
end
__END__