5c74fa857b63de7c61fc5211ed2828c994f392fc
[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 @@tablehead,@@tablefoot=[],[]
79 @env=SiSU_Env::InfoEnv.new(@md.fns)
80 @base_url="#{@env.url.root}/#{@md.fnb}/#{@hname}.html"
81 end
82 def urls(data)
83 @words=[]
84 map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map
85 data.each do |word|
86 @words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
87 if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
88 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/.match(word).captures
89 elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
90 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/.match(word).captures
91 elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}image/
92 m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(image)/.match(word).captures
93 end
94 word=case m
95 when /\.png|\.jpg|\.gif|c=|\d+x\d+/
96 w,h=/(\d+)x(\d+)/.match(m).captures if m =~/\d+x\d+/
97 w=%{width="#{w}"} if w
98 h=%{height="#{h}"} if h
99 c=m[/"(.+?)"/m,1]
100 caption=%{<br><p class="caption">#{c}</p>} if c
101 png=m.scan(/\S+/)[0]
102 ins=if u \
103 and u.strip !~/^image$/
104 %{<a href="#{u}">[#{png}]</a>#{caption}}
105 else %{[#{png}] #{caption}}
106 end
107 word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/,ins)
108 else
109 u=case u
110 when /^https?:\/\//
111 u
112 when /^:/
113 u=u.gsub(/^:/,'')
114 "#{@env.url.root}/#{u}"
115 when /^\.\.\// # can remove
116 u=u.gsub(/^\.\.\//,'')
117 "#{@env.url.root}/#{u}"
118 else
119 if not map_nametags[u].nil?
120 @env.url.root + '/' \
121 + @md.fnb + '/' \
122 + map_nametags[u][:segname] \
123 + Sfx[:html] \
124 + '#' + u
125 else ''
126 end
127 end
128 link=m[/(.+)/m]
129 png=m.scan(/\S+/)[0].strip
130 link=link.strip
131 ins=%{<a href="#{u}">#{link}</a>}
132 word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,ins).
133 gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,ins)
134 word
135 end
136 word
137 else word
138 end
139 word
140 end
141 @words=@words.join(' ')
142 end
143 def markup_generic(s)
144 s=s.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'<b>\1</b>').
145 gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'<i>\1</i>').
146 gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'<u>\1</u>').
147 gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'"\1"').
148 gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'+{\1}+').
149 gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strke_c]}/,'-{\1}-').
150 gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'<sup>\1</sup>').
151 gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'<sub>\1</sub>').
152 gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'<tt>\1</tt>'). # tt, kbd
153 gsub(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/i,'~')
154 end
155 def markup_object(t_o)
156 s=t_o.obj
157 s=if t_o.is !=:code
158 s=markup_generic(s)
159 if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
160 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+/)
161 words=urls(wm)
162 s=s.gsub(/.+/m,words)
163 end
164 s.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
165 gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;').
166 gsub(/#{Mx[:url_o]}[_\\](\S+?)#{Mx[:url_c]}/,'<a href="\1" target="_top">\1</a>'). #http ftp matches escaped, no decoration
167 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
168 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
169 else
170 s.gsub(/</m,'&lt;').
171 gsub(/>/m,'&gt;')
172 end
173 s
174 end
175 def markup_note(s)
176 s=markup_generic(s)
177 if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
178 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+/)
179 words=urls(wm)
180 s=s.gsub(/.+/m,words)
181 end
182 s=s.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
183 gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;').
184 gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'<a href="\1" target="_top">\1</a>\2'). #http ftp matches escaped, no decoration
185 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
186 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
187 end
188 def paragraph
189 %{<p class="h#{@lv}" type="substantive" header="#{@hname}">#{@txt}</p>\n} # << "\n"
190 end
191 def endnote(nr,en) #used only by db
192 txt=markup_note(en)
193 <<GSUB
194 <p class="endnote" name="note_#{nr}" from="#{@t_o.ocn}">
195 <a href="#{@base_url}#-#{nr}" name="_#{nr}">#{nr}.</a> <note>#{txt}</note>
196 </p>
197 GSUB
198 end
199 def tag_header(h)
200 %{<p class="#{h[:class]}" type="#{h[:type]}" header="#{h[:header]}">#{h[:txt]}</a></p>\n} # << "\n"
201 end
202 def tag_para(h)
203 %{<p class="#{h[:class]}" type="#{h[:type]}">#{h[:txt]}</a></p>\n} << "\n"
204 end
205 def lev_toc_hname
206 %{<p class="toc#{@lv}" header="#{@hname}"><a href="##{@ocn}">#{@txt}</a></p>\n} #<< "\n"
207 end
208 def lev_toc
209 h={ txt: txt, class: "toc#{@lv}", type: 'toc' }
210 tag_para(h)
211 end
212 def lev4_plus
213 txt=markup_object(@t_o)
214 h={ txt: txt, class: "h#{@lv}", type: 'substantive', id: @ocn, header: @hname }
215 tag_header(h)
216 end
217 def lev4_minus
218 txt=markup_object(@t_o)
219 h={ txt: txt, class: "h#{@t_o.ln}", type: 'substantive', id: @ocn }
220 tag_para(h)
221 end
222 def norm_comment
223 h={ txt: @t_o.obj, class: 'norm', type: 'comment' }
224 tag_para(h)
225 end
226 def norm
227 txt=markup_object(@t_o)
228 h={ txt: txt, class: 'norm', type: 'substantive', id: @ocn }
229 tag_para(h)
230 end
231 def code
232 txt=markup_object(@t_o)
233 h={ txt: "<tt>#{txt}</tt>", class: 'code', type: 'substantive', id: @ocn }
234 tag_para(h)
235 end
236 def indent(t)
237 txt=markup_object(@t_o)
238 h={ txt: txt, class: "indent#{t}", type: 'substantive', id: @ocn }
239 tag_para(h)
240 end
241 def hang_indent(f,t)
242 txt=markup_object(@t_o)
243 h={ txt: txt, class: "hang#{f}indent#{t}", type: 'substantive', id: @ocn }
244 #h={ txt: txt, class: "h#{f}i#{t}", type: 'substantive', id: @ocn }
245 tag_para(h)
246 end
247 def para_table
248 %{<p class="norm" align="left"><font #{the_font.set_small} #{the_font.set_color} #{the_font.set_face}>}
249 end
250 def ocn
251 %{<label class="ocn">#{@ocn}</label>} << "\n"
252 end
253 def html_table # get rid of use html_table
254 @new_content=[]
255 @txt.split(/\n/).each do |parablock|
256 m=parablock[/<!f(.+?)!>/,1]
257 @@tablefoot << m if m
258 parablock=parablock.gsub(/<!f.+?!>/,'')
259 @@tablehead=1 if parablock =~/#{Mx[:gr_o]}Th#{Mx[:tc_p]}/u
260 parablock=parablock.gsub(/#{Mx[:gr_o]}Th?#{Mx[:tc_p]}.+?#{Mx[:tc_p]}~(\d+)#{Mx[:gr_c]}/,
261 %{<table summary="normal text css" width="100%" border="0" cellpadding="2" align="center">})
262 if parablock =~/#{Mx[:gr_o]}TZ#{Mx[:gr_c]}/
263 tablefoot=[]
264 @@tablefoot.each {|x| tablefoot << %{<p align="center"><font size=2><i>#{x}</i></font></p>\n}}
265 @@tablefoot=[]
266 parablock=parablock.gsub(/#{Mx[:gr_o]}TZ#{Mx[:gr_c]}/,
267 %{#{the_table_close}\n}) # +
268 end
269 if @@tablehead==1
270 if parablock =~/#{Mx[:tc_p]}#{Mx[:tc_p]}/u
271 parablock=parablock.gsub(/#{Mx[:tc_o]}#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
272 %{\n<tr>} +
273 %{\n<td width="\\1%" valign="top">} +
274 %{#{para_table}<b>}).
275 gsub(/#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
276 %{</b></td><td width="\\1%" valign="top">} +
277 %{#{para_table}<b>}).
278 gsub(/#{Mx[:tc_c]}/, '</b></td></tr>')
279 @@tablehead=0
280 end
281 parablock
282 else
283 parablock=parablock.gsub(/#{Mx[:tc_o]}#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
284 %{\n<tr>} +
285 %{\n<td width="\\1%" valign="top">} +
286 %{#{para_table}}).
287 gsub(/#{Mx[:tc_p]}#{Mx[:tc_p]}(\d+?)#{Mx[:tc_p]}/u,
288 %{</td><td width="\\1%" valign="top">} +
289 %{#{para_table}}).
290 gsub(/#{Mx[:tc_c]}/, '</td></tr>')
291 parablock
292 end
293 @new_content << parablock
294 end
295 @new_content.join
296 end
297 end
298 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
299 def initialize(attrib='',txt='',id=nil,ocnd=nil,ocns=nil,lv='',hname=nil)
300 @tab="\t"
301 @attrib=attrib
302 @txt=txt
303 @lv=lv.to_s
304 @hname=hname.to_s
305 @id=@ocn=id
306 end
307 def paragraph
308 attrib=%{class="#{@attrib}" }
309 if @ocn
310 id=%{id="#{Mx[:ocn_id_char]}#{@ocn}" }
311 type=%{type="substantive" }
312 else
313 id=''
314 type=%{type="comment" }
315 end
316 header=%{header="#{@hname}" } if @hname
317 %{<p #{attrib}#{type}#{header}>#{@txt}</p>\n} #<< "\n"
318 end
319 def para
320 paragraph
321 end
322 end
323 end
324 __END__