json, an output representation, first pass
[software/sisu] / lib / sisu / json_format.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** Description: json output logic, flow
8
9 ** Author: Ralph Amissah
10 [ralph@amissah.com]
11 [ralph.amissah@gmail.com]
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see [http://www.gnu.org/licenses/].
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 [http://www.fsf.org/licensing/licenses/gpl.html]
39 [http://www.gnu.org/licenses/gpl.html]
40
41 ** SiSU uses:
42 * Standard SiSU markup syntax,
43 * Standard SiSU meta-markup syntax, and the
44 * Standard SiSU object citation numbering and system
45
46 ** Hompages:
47 [http://www.jus.uio.no/sisu]
48 [http://www.sisudoc.org]
49
50 ** Git
51 [http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary]
52 [http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/json_format.rb;hb=HEAD]
53
54 =end
55 module SiSU_JSON_Format
56 require_relative 'dp' # dp.rb
57 require_relative 'json_parts' # json_parts.rb
58 include SiSU_Param
59 class ParagraphNumber
60 def initialize(md,paranum)
61 @md=md
62 @paranum=(paranum \
63 ? (/(\d+)/m.match(paranum)[1])
64 : nil)
65 end
66 def display
67 p_num_display=if @paranum
68 @paranum.gsub(/(\d+)/,
69 '<font size="1" color="#777777">' +
70 '&nbsp;&nbsp;\1</font>')
71 else ''
72 end
73 p_num_display
74 end
75 def name
76 p_num_name=@paranum.gsub(/(\d+)/,'<a name="\1"></a>')
77 p_num_name
78 end
79 def goto
80 p_num_goto=@paranum.gsub(/(\d+)/,'<a href="#\1">')
81 p_num_goto
82 end
83 end
84 class HeadInformation
85 include SiSU_Parts_JSON
86 def initialize #dc rdf
87 @full_title=@subtitle=@author=@subject=@description=@publisher=@contributor=@date=@type=@format=@identifier=@source=@language=@relation=@coverage=@rights=@copyright=@owner=@keywords=''
88 @md=@@md
89 # DublinCore 1 - title
90 @rdfurl=%{ rdf:about="http://www.jus.uio.no/lm/toc"\n}
91 if defined? @md.title.full \
92 and @md.title.full # DublinCore 1 - title
93 @rdf_title=%{ dc.title="#{seg_name}#{@md.title.full}"\n}
94 @full_title=%{<meta name="dc.title" content="#{seg_name}#{@md.title.full}" />\n}
95 end
96 if defined? @md.creator.author \
97 and @md.creator.author # DublinCore 2 - creator/author (author)
98 @rdf_author=%{ dc.author="#{@md.creator.author}"\n}
99 @author=%{<meta name="dc.author" content="#{@md.creator.author}" />\n}
100 end
101 if defined? @md.classify.subject \
102 and @md.classify.subject=~/\S+/ # DublinCore 3 - subject (us library of congress, eric or udc, or schema???)
103 @rdf_subject=%{ dc.subject="#{@md.classify.subject}"\n}
104 @subject=%{<meta name="dc.subject" content="#{@md.classify.subject}" />\n}
105 end
106 if defined? @md.notes.description \
107 and @md.notes.description=~/\S+/ # DublinCore 4 - description
108 @rdf_description=%{ dc.description="#{@md.notes.description}"\n}
109 @description=%{<meta name="dc.description" content="#{@md.notes.description}" />\n}
110 end
111 if defined? @md.publisher \
112 and @md.publisher=~/\S+/ # DublinCore 5 - publisher (current copy published by)
113 @rdf_publisher=%{ dc.publisher="#{@md.publisher}"\n}
114 @publisher=%{<meta name="dc.publisher" content="#{@md.publisher}" />\n}
115 end
116 if defined? @md.creator.contributor \
117 and @md.creator.contributor=~/\S+/ # DublinCore 6 - contributor
118 @rdf_contributor=%{ dc.contributor="#{@md.creator.contributor}"\n}
119 @contributor=%{<meta name="dc.contributor" content="#{@md.creator.contributor}" />\n}
120 end
121 if defined? @md.date.published \
122 and @md.date.published # DublinCore 7 - date year-mm-dd
123 @rdf_date=%{ dc.date="#{@md.date.published}"\n}
124 @date=%{<meta name="dc.date" content="#{@md.date.published}" #{@md.date_scheme} />\n}
125 end
126 if defined? @md.date.created \
127 and @md.date.created # DublinCore 7 - date.created year-mm-dd
128 @rdf_date_created=%{ dc.date.created="#{@md.date.created}"\n}
129 @date_created=%{<meta name="dc.date.created" content="#{@md.date.created}" #{@md.date_created_scheme} />\n}
130 end
131 if defined? @md.date.issued \
132 and @md.date.issued # DublinCore 7 - date.issued year-mm-dd
133 @rdf_date_issued=%{ dc.date.issued="#{@md.date.issued}"\n}
134 @date_issued=%{<meta name="dc.date.issued" content="#{@md.date.issued}" #{@md.date_issued_scheme} />\n}
135 end
136 if defined? @md.date.available \
137 and @md.date.available # DublinCore 7 - date.available year-mm-dd
138 @rdf_date_available=%{ dc.date.available="#{@md.date.available}"\n}
139 @date_available=%{<meta name="dc.date.available" content="#{@md.date.available}" #{@md.date_available_scheme} />\n}
140 end
141 if defined? @md.date.valid \
142 and @md.date.valid # DublinCore 7 - date.valid year-mm-dd
143 @rdf_date_valid=%{ dc.date.valid="#{@md.date.valid}"\n}
144 @date_valid=%{<meta name="dc.date.valid" content="#{@md.date.valid}" #{@md.date_valid_scheme} />\n}
145 end
146 if defined? @md.date.modified \
147 and @md.date.modified # DublinCore 7 - date.modified year-mm-dd
148 @rdf_date_modified=%{ dc.date.modified="#{@md.date.modified}"\n}
149 @date_modified=%{<meta name="dc.date.modified" content="#{@md.date.modified}" #{@md.date_modified_scheme} />\n}
150 end
151 if defined? @md.notes.coverage \
152 and @md.notes.coverage=~/\S+/ # DublinCore 14 - coverage
153 @rdf_coverage=%{ dc.coverage="#{@md.notes.coverage}"\n}
154 @coverage=%{<meta name="dc.coverage" content="#{@md.notes.coverage}" />\n}
155 end
156 if defined? @md.notes.relation \
157 and @md.notes.relation=~/\S+/ # DublinCore 13 - relation
158 @rdf_relation=%{ dc.relation="#{@md.notes.relation}"\n}
159 @relation=%{<meta name="dc.relation" content="#{@md.notes.relation}" />\n}
160 end
161 if defined? @md.notes.type \
162 and @md.notes.type # DublinCore 8 - type (genre eg. report, convention etc)
163 @rdf_type=%{ dc.type="#{@md.notes.type}"\n}
164 @type=%{<meta name="dc.type" content="#{@md.notes.type}" />\n}
165 end
166 if defined? @md.notes.format \
167 and @md.notes.format=~/\S+/ # DublinCore 9 - format (use your mime type)
168 @rdf_format=%{ dc.format="#{@md.notes.format}"\n}
169 @format=%{<meta name="dc.format" content="#{@md.notes.format}" />\n}
170 end
171 #if defined? @md.identifier.sisupod \
172 #and @md.identifier.sisupod=~/\S+/ # DublinCore 10 - identifier (your identifier, could use urn which is free)
173 # @rdf_identifier=%{ dc.identifier="#{@md.identifier.sisupod}"\n}
174 # @identifier=%{<meta name="dc.identifier" content="#{@md.identifier.sisupod}" />\n}
175 #end
176 if defined? @md.original.source \
177 and @md.original.source=~/\S+/ # DublinCore 11 - source (document source)
178 @rdf_source=%{ dc.source="#{@md.original.source}"\n}
179 @source=%{<meta name="dc.source" content="#{@md.source}" />\n}
180 end
181 if defined? @md.original.language \
182 and @md.original.language=~/\S+/ # DublinCore 12 - language (English)
183 @rdf_language=%{ dc.language="#{@md.original.title}"\n}
184 @language=%{<meta name="dc.language" content="#{@md.language[:name]}" />\n}
185 end
186 if defined? @md.rights.all \
187 and @md.rights.all=~/\S+/ # DublinCore 15 - rights
188 rights=meta_content_clean(@md.rights.all)
189 copyright=meta_content_clean(@md.rights.copyright.all)
190 @rdf_rights=%{ dc.rights="#{rights}"\n}
191 @rights=%{<meta name="dc.rights" content="#{rights}" />\n}
192 end
193 @copyright=%{<meta name="copyright" content="#{copyright}" />\n} \
194 if @md.rights.copyright.all # possibly redundant see dc.rights
195 @owner=%{<meta name="owner" content="#{@md.owner}" />\n} if @md.owner
196 @keywords=%{<meta name="keywords" content="#{@md.keywords}" />\n} if @md.keywords
197 @index='index'
198 end
199 def meta_content_clean(content='')
200 content=if not content.nil?
201 content=content.tr('"',"'").
202 gsub(/&/,'&amp;')
203 content=SiSU_XML_Munge::Trans.new(@md).char_enc.utf8(content)
204 else content
205 end
206 end
207 def table_close
208 '</font> </td></tr></table>'
209 end
210 def toc_head
211 <<WOK
212 <html>
213 <head>
214 <title>#{@md.html_title}</title>
215 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
216 xmlns:dc="http://purl.org/dc/elements/1.1/">
217 <rdf:Description
218 #{@rdfurl}
219 #{@rdf_title}
220 #{@rdf_subtitle}
221 #{@rdf_author}
222 #{@rdf_subject}
223 #{@rdf_description}
224 #{@rdf_publisher}
225 #{@rdf_contributor}
226 #{@rdf_date}
227 #{@rdf_date_created}
228 #{@rdf_date_issued}
229 #{@rdf_date_available}
230 #{@rdf_date_valid}
231 #{@rdf_date_modified}
232 #{@rdf_type}
233 #{@rdf_format}
234 #{@rdf_identifier}
235 #{@rdf_source}
236 #{@rdf_language}
237 #{@rdf_relation}
238 #{@rdf_coverage}
239 #{@rdf_rights}
240 />
241 </rdf:RDF>
242 #{@full_title}
243 #{@author}
244 #{@subject}
245 #{@description}
246 #{@publisher}
247 #{@contributor}
248 #{@date}
249 #{@date_created}
250 #{@date_issued}
251 #{@date_available}
252 #{@date_valid}
253 #{@date_modified}
254 #{@type}
255 #{@format}
256 #{@identifier}
257 #{@source}
258 #{@language}
259 #{@relation}
260 #{@coverage}
261 #{@rights}
262 #{@copyright}
263 #{@owner}
264 #{@png.ico}
265 #{@txt.generator}
266 #{@js.head}
267 \n</head>
268 #{@color.body}
269 #{@font.css_table_file}
270 <a name="top"></a>
271 <a name="up"></a>
272 <a name="start"></a>
273 #{@js.top}
274 WOK
275 end
276 end
277 class ParagraphNumber
278 def initialize(md,ocn)
279 @md,@ocn=md,ocn.to_s
280 @ocn ||=''
281 end
282 def ocn_display
283 @make=SiSU_Env::ProcessingSettings.new(@md)
284 if @make.build.ocn?
285 ocn_class='ocn'
286 if @ocn.to_i==0
287 @ocn.gsub(/^(\d+|)$/,
288 %{<label class="#{ocn_class}"><a name="#{@ocn}">&nbsp;</a></label>})
289 else
290 @ocn.gsub(/^(\d+|)$/,
291 %{<label class="#{ocn_class}"><a name="#{@ocn}">\\1</a></label>})
292 end
293 else
294 ocn_class='ocn_off'
295 @ocn.gsub(/^(\d+|)$/,
296 %{<label class="#{ocn_class}">&nbsp;</label>})
297 end
298 end
299 def name
300 %{<a name="#{@ocn}"></a>}
301 end
302 def id #w3c? "tidy" complains about numbers as identifiers ! annoying
303 %{id="o#{@ocn}"}
304 end
305 def goto
306 %{<a href="##{@ocn}">}
307 end
308 end
309 class FormatTextObject
310 include SiSU_Parts_JSON
311 attr_accessor :md,:dob,:txt,:ocn,:format,:table,:link,:linkname,:paranum,:p_num,:headname,:banner,:url
312 def initialize(md,t_o)
313 @md,@t_o=md,t_o
314 if t_o.class.inspect =~/Object/
315 @txt=if defined? t_o.obj; t_o.obj
316 else nil
317 end
318 @ocn=if defined? t_o.ocn; t_o.ocn.to_s
319 else nil
320 end
321 @headname=if t_o.is==:heading and defined? t_o.name; t_o.name
322 else nil
323 end
324 else
325 if @md.opt.act[:maintenance][:set]==:on
326 p __FILE__ << ':' << __LINE__.to_s
327 p t_o.class
328 p caller
329 end
330 end
331 if defined? @t_o.ocn
332 ocn=((@t_o.ocn.to_s =~/\d+/) ? @t_o.ocn : nil)
333 @p_num=ParagraphNumber.new(@md,ocn)
334 end
335 if @format and not @format.empty?
336 if @format=~/^\d:(\S+)/ #need more reliable marker #if @format =~ /#{Rx[:lv]}/
337 headname=$1 #format[/\d~(\S+)/m,1]
338 @headname=if headname =~/^[a-zA-Z]/; %{<a name="#{headname}" id="#{headname}"></a>} #consider: h_#{headname}
339 else %{<a name="h#{headname}" id="h#{headname}"></a>}
340 end
341 end
342 end
343 @dob=t_o if defined? t_o.is
344 end
345 def para
346 para_form_css('p','norm')
347 end
348 def code
349 para_form_css('p','code')
350 end
351 def center
352 para_form_css('p','center')
353 end
354 def bold
355 para_form_css('p','bold')
356 end
357 def bullet
358 para_form_css('li','bullet')
359 end
360 def format(tag,attrib)
361 para_form_css(tag,attrib)
362 end
363 def heading_normal(tag,attrib)
364 %{
365 <div class="substance">
366 #{@p_num.ocn_display}
367 <#{tag} class="#{attrib}" #{@p_num.id}>#{@p_num.name}
368 #{@headname}#{@txt}
369 </#{tag}>
370 </div>
371 }
372 end
373 def heading_body
374 heading_normal('p','norm')
375 end
376 def heading_body0
377 heading_normal('h1','norm')
378 end
379 def heading_body1
380 heading_normal('h1','norm')
381 end
382 def heading_body2
383 heading_normal('h2','norm')
384 end
385 def heading_body3
386 heading_normal('h3','norm')
387 end
388 def heading_body4
389 heading_normal('h4','norm')
390 end
391 def heading_body5
392 heading_normal('h5','norm')
393 end
394 def heading_body6
395 heading_normal('h6','norm')
396 end
397 def heading_body7
398 heading_normal('h7','norm')
399 end
400 def title_header(tag,attrib)
401 %{
402 <div class="content">
403 <#{tag} class="#{attrib}">
404 #{@txt}
405 </#{tag}>
406 </div>
407 }
408 end
409 def title_header1
410 title_header('h1','tiny')
411 end
412 def title_header2
413 title_header('h2','tiny')
414 end
415 def title_header3
416 title_header('h3','tiny')
417 end
418 def title_header4
419 ''
420 end
421 def dl #check :trailer
422 "<dl><b>#{@txt}</b> #{@trailer}</dl>"
423 end
424 def table_css_end #<!TZ!>
425 '</table>
426 </p>
427 </div>'
428 end
429 def gsub_body
430 #fix
431 @txt=case @txt
432 when /^\s*\((i+|iv|v|vi+|ix|x|xi+)\)/
433 @txt.gsub(/^\((i+|iv|v|vi+|ix|x|xi+)\)/,'<b>(\1)</b>').
434 gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((i+|iv|v|vi+|ix|x|xi+)\)/,'\1<b>(\2)</b>')
435 when /^\s*\(?(\d|[a-z])+\)/
436 @txt.gsub(/^\((\d+|[a-z])+\)/,'<b>(\1)</b>').
437 gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((\d+|[a-z])+\)/,'\1<b>(\2)</b>')
438 when /^\s*\d{1,3}\.\s/
439 @txt.gsub(/^\s*(\d+\.)/,'<b>\1</b>')
440 when /^\s*[A-Z]\.\s/
441 @txt.gsub(/^\s*([A-Z]\.)/,'<b>\1</b>')
442 else @txt
443 end
444 end
445 def bold_para
446 %{#{the_margin.txt_0}
447 <p class="bold">
448 #{@txt}
449 </p>
450 #{the_margin.num_css}
451 &nbsp;&nbsp;&nbsp;
452 #{the_table_close}}
453 end
454 def bold_header
455 @txt=@txt.gsub(/[1-9]~(\S+)/,'<a name="\1"></a>').
456 gsub(/[1-9]~/,'')
457 %{<p class="bold">
458 #{@txt}
459 </p>
460 #{the_margin.num_css}
461 &nbsp;&nbsp;&nbsp;
462 #{the_table_close}}
463 end
464 def toc_head_copy_at
465 %{<p class="center">#{@txt}</p>\n}
466 end
467 def center
468 %{<p class="center">#{@txt}</p>\n}
469 end
470 def bold
471 %{<p class="bold">#{@txt}</p>\n}
472 end
473 def center_bold
474 %{<p class="centerbold">#{@txt}</p>\n}
475 end
476 end
477 end
478 __END__