65c0537d8f544ac3197eaef4b06523c484a27139
[software/sisu] / lib / sisu / develop / html_lite_shared.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** modules shared by db and flatfile output generators, mostly xml/xhtml/html
8 formatting
9
10 ** Author: Ralph Amissah
11 <ralph@amissah.com>
12 <ralph.amissah@gmail.com>
13
14 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
15 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
16 All Rights Reserved.
17
18 ** License: GPL 3 or later:
19
20 SiSU, a framework for document structuring, publishing and search
21
22 Copyright (C) Ralph Amissah
23
24 This program is free software: you can redistribute it and/or modify it
25 under the terms of the GNU General Public License as published by the Free
26 Software Foundation, either version 3 of the License, or (at your option)
27 any later version.
28
29 This program is distributed in the hope that it will be useful, but WITHOUT
30 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32 more details.
33
34 You should have received a copy of the GNU General Public License along with
35 this program. If not, see <http://www.gnu.org/licenses/>.
36
37 If you have Internet connection, the latest version of the GPL should be
38 available at these locations:
39 <http://www.fsf.org/licensing/licenses/gpl.html>
40 <http://www.gnu.org/licenses/gpl.html>
41
42 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
43
44 ** SiSU uses:
45 * Standard SiSU markup syntax,
46 * Standard SiSU meta-markup syntax, and the
47 * Standard SiSU object citation numbering and system
48
49 ** Hompages:
50 <http://www.jus.uio.no/sisu>
51 <http://www.sisudoc.org>
52
53 ** Git
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
55 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/html_lite_shared.rb;hb=HEAD>
56
57 =end
58 module SiSU_FormatShared
59 require_relative 'html_parts' # html_parts.rb
60 class CSS_Format
61 require_relative 'se_hub_particulars' # se_hub_particulars.rb
62 include SiSU_Parts_HTML
63 @@fns=nil
64 def initialize(md,t_o)
65 @md,@t_o=md,t_o
66 @txt=@t_o.obj
67 @id=@ocn=@t_o.ocn if defined? @t_o.ocn
68 @lv=@t_o.lv.to_s if @t_o.is==:heading
69 if @md.fns != @@fns
70 @@fns,@@hname=@md.fns,''
71 end
72 @hname=if defined? @t_o.name \
73 and not @t_o.name.to_s.empty?
74 @@hname=@t_o.name
75 else @@hname
76 end
77 @tab="\t"
78 @ocn_html_identifier=SiSU_Env::ProcessingSettings.new(@md).ocn_html_identifier
79 @@tablehead,@@tablefoot=[],[]
80 @env=SiSU_Env::InfoEnv.new(@md.fns)
81 @base_url="#{@env.url.root}/#{@md.fnb}/#{@hname}.html"
82 end
83 def urls(data)
84 @words=[]
85 map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map
86 data.each do |word|
87 @words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
88 if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
89 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/.match(word).captures
90 elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
91 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/.match(word).captures
92 elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}image/
93 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(image)/.match(word).captures
94 end
95 word=case m
96 when /\.png|\.jpg|\.gif|c=|\d+x\d+/
97 w,h=/(\d+)x(\d+)/.match(m).captures if m =~/\d+x\d+/
98 w=%{width="#{w}"} if w
99 h=%{height="#{h}"} if h
100 c=m[/"(.+?)"/m,1]
101 caption=%{<br><p class="caption">#{c}</p>} if c
102 png=m.scan(/\S+/)[0]
103 ins=if u \
104 and u.strip !~/^image$/
105 %{<a href="#{u}">[#{png}]</a>#{caption}}
106 else %{[#{png}] #{caption}}
107 end
108 word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/,ins)
109 else
110 u=case u
111 when /^https?:\/\//
112 u
113 when /^:/
114 u=u.gsub(/^:/,'')
115 "#{@env.url.root}/#{u}"
116 when /^\.\.\// # can remove
117 u=u.gsub(/^\.\.\//,'')
118 "#{@env.url.root}/#{u}"
119 else
120 if not map_nametags[u].nil?
121 @env.url.root + '/' \
122 + @md.fnb + '/' \
123 + map_nametags[u][:segname] \
124 + Sfx[:html] \
125 + '#' + u
126 else ''
127 end
128 end
129 link=m[/(.+)/m]
130 png=m.scan(/\S+/)[0].strip
131 link=link.strip
132 ins=%{<a href="#{u}">#{link}</a>}
133 word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,ins).
134 gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,ins)
135 word
136 end
137 word
138 else word
139 end
140 word
141 end
142 @words=@words.join(' ')
143 end
144 def markup_generic(s)
145 s=s.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'<b>\1</b>').
146 gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'<i>\1</i>').
147 gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'<u>\1</u>').
148 gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'"\1"').
149 gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'+{\1}+').
150 gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strke_c]}/,'-{\1}-').
151 gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'<sup>\1</sup>').
152 gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'<sub>\1</sub>').
153 gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'<tt>\1</tt>'). # tt, kbd
154 gsub(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/i,'~')
155 end
156 def markup_object(t_o)
157 s=t_o.obj
158 s=if t_o.is !=:code
159 s=markup_generic(s)
160 if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
161 wm=s.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)|\S+/)
162 words=urls(wm)
163 s=s.gsub(/.+/m,words)
164 end
165 s.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
166 gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;').
167 gsub(/#{Mx[:url_o]}[_\\](\S+?)#{Mx[:url_c]}/,'<a href="\1" target="_top">\1</a>'). #http ftp matches escaped, no decoration
168 gsub(/(#{Mx[:lnk_c]})#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,'\1<a href="\2" target="_top">\2</a>'). #special case \{ e.g. \}http://url
169 gsub(/(^|#{Mx[:gl_c]}|\s)#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,%{\\1#{the_url_decoration.xml_open}<a href="\\2" target="_top">\\2</a>#{the_url_decoration.xml_close}\\3}) #http ftp matches with decoration
170 else
171 s.gsub(/</m,'&lt;').
172 gsub(/>/m,'&gt;')
173 end
174 s
175 end
176 def markup_note(s)
177 s=markup_generic(s)
178 if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
179 wm=s.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)|\S+/)
180 words=urls(wm)
181 s=s.gsub(/.+/m,words)
182 end
183 s=s.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
184 gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;').
185 gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'<a href="\1" target="_top">\1</a>\2'). #http ftp matches escaped, no decoration
186 gsub(/(#{Mx[:lnk_c]})#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,'\1<a href="\2" target="_top">\2</a>'). #special case \{ e.g. \}http://url
187 gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,%{#{the_url_decoration.xml_open}<a href="\\1" target="_top">\\1</a>#{the_url_decoration.xml_close}}) #http ftp matches with decoration
188 end
189 def paragraph
190 %{<p class="h#{@lv}" type="substantive" header="#{@hname}">#{@txt}</p>\n} # << "\n"
191 end
192 def endnote(nr,en) #used only by db
193 txt=markup_note(en)
194 <<GSUB
195 <p class="endnote" name="note_#{nr}" from="#{@t_o.ocn}">
196 <a href="#{@base_url}#-#{nr}" name="_#{nr}">#{nr}.</a> <note>#{txt}</note>
197 </p>
198 GSUB
199 end
200 def tag_header(h)
201 %{<p class="#{h[:class]}" type="#{h[:type]}" header="#{h[:header]}">#{h[:txt]}</a></p>\n} # << "\n"
202 end
203 def tag_para(h)
204 %{<p class="#{h[:class]}" type="#{h[:type]}">#{h[:txt]}</a></p>\n} << "\n"
205 end
206 def lev_toc_hname
207 %{<p class="toc#{@lv}" header="#{@hname}"><a href="##{@ocn_html_identifier}#{@ocn}">#{@txt}</a></p>\n} #<< "\n"
208 end
209 def lev_toc
210 h={ txt: txt, class: "toc#{@lv}", type: 'toc' }
211 tag_para(h)
212 end
213 def lev4_plus
214 txt=markup_object(@t_o)
215 h={ txt: txt, class: "h#{@lv}", type: 'substantive', id: @ocn, header: @hname }
216 tag_header(h)
217 end
218 def lev4_minus
219 txt=markup_object(@t_o)
220 h={ txt: txt, class: "h#{@t_o.ln}", type: 'substantive', id: @ocn }
221 tag_para(h)
222 end
223 def norm_comment
224 h={ txt: @t_o.obj, class: 'norm', type: 'comment' }
225 tag_para(h)
226 end
227 def norm
228 txt=markup_object(@t_o)
229 h={ txt: txt, class: 'norm', type: 'substantive', id: @ocn }
230 tag_para(h)
231 end
232 def code
233 txt=markup_object(@t_o)
234 h={ txt: "<tt>#{txt}</tt>", class: 'code', type: 'substantive', id: @ocn }
235 tag_para(h)
236 end
237 def indent(t)
238 txt=markup_object(@t_o)
239 h={ txt: txt, class: "indent#{t}", type: 'substantive', id: @ocn }
240 tag_para(h)
241 end
242 def hang_indent(f,t)
243 txt=markup_object(@t_o)
244 h={ txt: txt, class: "hang#{f}indent#{t}", type: 'substantive', id: @ocn }
245 #h={ txt: txt, class: "h#{f}i#{t}", type: 'substantive', id: @ocn }
246 tag_para(h)
247 end
248 def para_table
249 %{<p class="norm" align="left"><font #{the_font.set_small} #{the_font.set_color} #{the_font.set_face}>}
250 end
251 def ocn
252 %{<label class="ocn">#{@ocn}</label>} << "\n"
253 end
254 def html_table # get rid of use html_table
255 @new_content=[]
256 @txt.split(/\n/).each do |parablock|
257 m=parablock[/<!f(.+?)!>/,1]
258 @@tablefoot << m if m
259 parablock=parablock.gsub(/<!f.+?!>/,'')
260 @@tablehead=1 if parablock =~/#{Mx[:gr_o]}Th#{Mx[:tc_p]}/u
261 parablock=parablock.gsub(/#{Mx[:gr_o]}Th?#{Mx[:tc_p]}.+?#{Mx[:tc_p]}~(\d+)#{Mx[:gr_c]}/,
262 %{<table summary="normal text css" width="100%" border="0" cellpadding="2" align="center">})
263 if parablock =~/#{Mx[:gr_o]}TZ#{Mx[:gr_c]}/
264 tablefoot=[]
265 @@tablefoot.each {|x| tablefoot << %{<p align="center"><font size=2><i>#{x}</i></font></p>\n}}
266 @@tablefoot=[]
267 parablock=parablock.gsub(/#{Mx[:gr_o]}TZ#{Mx[:gr_c]}/,
268 %{#{the_table_close}\n}) # +
269 end
270 if @@tablehead==1
271 if parablock =~/#{Mx[:tc_p]}#{Mx[:tc_p]}/u
272 parablock=parablock.gsub(/#{Mx[:tc_o]}#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
273 %{\n<tr>} +
274 %{\n<td width="\\1%" valign="top">} +
275 %{#{para_table}<b>}).
276 gsub(/#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
277 %{</b></td><td width="\\1%" valign="top">} +
278 %{#{para_table}<b>}).
279 gsub(/#{Mx[:tc_c]}/, '</b></td></tr>')
280 @@tablehead=0
281 end
282 parablock
283 else
284 parablock=parablock.gsub(/#{Mx[:tc_o]}#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
285 %{\n<tr>} +
286 %{\n<td width="\\1%" valign="top">} +
287 %{#{para_table}}).
288 gsub(/#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
289 %{</td><td width="\\1%" valign="top">} +
290 %{#{para_table}}).
291 gsub(/#{Mx[:tc_c]}/, '</td></tr>')
292 parablock
293 end
294 @new_content << parablock
295 end
296 @new_content.join
297 end
298 end
299 class CSS_FormatGeneric #does CSS_Format in one definition, needs to be told about attrib, despite brevity of generic, easier to see structure with CSS_Format
300 def initialize(attrib='',txt='',id=nil,ocnd=nil,ocns=nil,lv='',hname=nil)
301 @tab="\t"
302 @attrib=attrib
303 @txt=txt
304 @lv=lv.to_s
305 @hname=hname.to_s
306 @id=@ocn=id
307 end
308 def paragraph
309 attrib=%{class="#{@attrib}" }
310 if @ocn
311 id=%{id="#{Mx[:ocn_id_char]}#{@ocn}" }
312 type=%{type="substantive" }
313 else
314 id=''
315 type=%{type="comment" }
316 end
317 header=%{header="#{@hname}" } if @hname
318 %{<p #{attrib}#{type}#{header}>#{@txt}</p>\n} #<< "\n"
319 end
320 def para
321 paragraph
322 end
323 end
324 end
325 __END__