d: po4a, continue reorganization (translation request dev stopped)
[software/sisu] / lib / sisu / develop / po4a_set.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** po4a
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 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 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/po4a_set.rb;hb=HEAD>
55
56 =end
57 module SiSU_Po4aUtils
58 class Wrap
59 def initialize(md,orig='',trans='',is_desc='',n_char_max=76,n_indent=0,n_hang=nil)
60 @md,@orig,@trans,@is_desc,@n_char_max,@n_indent=
61 md, orig, trans, is_desc, n_char_max, n_indent
62 @n_char_max_extend = n_char_max
63 @br="\n"
64 @n_hang=n_hang ? n_hang : @n_indent
65 @po4a_identify_type='type: SiSU doc'
66 #@po4a_identify_type='type: Plain text'
67 end
68 def line_wrap
69 space=' '
70 spaces_indent,spaces_hang=
71 "#{@br}#{space*@n_indent}",space*@n_hang
72 pot,i=[],0
73 pot_array=(@trans.empty?) ? [@orig] : [@orig,@trans]
74 pot_array.each do |pa|
75 line=0
76 out=[]
77 out[line]=''
78 @oldword='' #REMOVE @oldword
79 pa=pa.gsub(/<br>/,' <br> ').
80 gsub(/#{Mx[:br_nl]}/,"\n\n")
81 words=pa.scan(/\n\n|\\\\\\|<br>|\S+/m)
82 while words != ''
83 word=words.shift
84 if not word
85 out[line] unless out[line].empty? #check
86 break
87 elsif word =~/\n\n/
88 word="\n"
89 @n_char_max_extend = @n_char_max + out[line].length
90 line=line
91 elsif (out[line].length + word.length) > (@n_char_max_extend - @n_indent) \
92 and out[line] =~/\S+/
93 @n_char_max_extend = @n_char_max
94 out[line].squeeze!(' ')
95 line += 1
96 end
97 if word
98 out[line]=if out[line] \
99 and out[line] !~/\S+$/m
100 "#{out[line]}#{word}"
101 elsif out[line] \
102 and out[line] =~/\S+/
103 "#{out[line]} #{word}"
104 else "#{word.strip}"
105 end
106 end
107 @oldword=word if word =~/\S+/
108 end
109 x=out.join(spaces_indent).gsub(/\A\n+/m,'').insert(0,spaces_hang)
110 z=[]
111 x.split(/\n/).each do |y|
112 y=y.gsub(/"/,'\"')
113 y=%{"#{y}"}
114 z << y
115 end
116 pot[i]=z.join("\n")
117 i +=1
118 pot
119 end
120 trans=(pot.length == 2) ? pot[1] : ''
121 po_str=<<WOK
122 #. #{@po4a_identify_type} - #{@is_desc}
123 #: en/#{@md.fns}:#{PotNumber.new.num}
124 msgid ""
125 #{pot[0]}
126 msgstr ""
127 #{trans}
128 WOK
129 po_str
130 end
131 def no_line_wrap_block
132 pot,i=[],0
133 pot_array=(@trans.empty?) ? [@orig] : [@orig,@trans]
134 pot_array.each do |pa|
135 z=[]
136 pa.split(/\n\n/).each do |y|
137 y=y.gsub(/"/,'\"')
138 y=%{"#{y}"}
139 z << y if not y.empty?
140 end
141 pot[i]=z.join("\n")
142 i +=1
143 pot
144 end
145 trans=(pot.length == 2) ? pot[1] : ''
146 po_str=<<WOK
147 #. #{@po4a_identify_type} - #{@is_desc}
148 #: en/#{@md.fns}:#{PotNumber.new.num}
149 #, no-wrap
150 msgid ""
151 #{pot[0]}
152 msgstr ""
153 #{trans}
154 WOK
155 po_str
156 end
157 def line_wrap_indent1
158 @n_indent,@n_hang=2,2
159 line_wrap
160 end
161 def line_wrap_endnote
162 @n_indent,@n_hang=4,2
163 line_wrap
164 end
165 def array_wrap
166 if @orig.is_a?(Array)
167 @arr=[]
168 @orig.each do |line|
169 @arr << SiSU_TextUtils::Wrap.new(line,@n_char_max,@n_indent,@n_hang).line_wrap
170 end
171 end
172 @arr
173 end
174 end
175 class HeaderScan
176 def initialize(md,para)
177 @md,@p=md,para
178 end
179 def extract(tag,tag_content,type,attrib)
180 if dc_tag \
181 and dc_content
182 [dc_tag,dc_content,{dc_tag=>dc_content}]
183 else nil
184 end
185 end
186 def header(tag,tag_content,type='',attrib='') #this will break stuff and must be tested thoroughly 20060825
187 @tag,@tag_content,@type,@attrib=tag,tag_content,type,attrib
188 def label #element
189 @tag
190 end
191 def type
192 @type
193 end
194 def text
195 @tag_content
196 end
197 def info #element text
198 @tag_content
199 end
200 def attribute
201 @attrib
202 end
203 def element
204 @tag
205 end
206 def attrib
207 @attrib
208 end
209 def el
210 @tag
211 end
212 self
213 end
214 def start_is_match
215 case @p
216 when /^#{Mx[:meta_o]}(title)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,@md.title.full,'meta','dc') #dc 1
217 when /^#{Mx[:meta_o]}(creator|author)#{Mx[:meta_c]}\s*(.+?)$/ then header('creator',$2,'meta','dc') #dc 2
218 when /^#{Mx[:meta_o]}(subject)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 3
219 when /^#{Mx[:meta_o]}(description)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 4
220 when /^#{Mx[:meta_o]}(publisher)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 5
221 when /^#{Mx[:meta_o]}(contributor)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 6
222 when /^#{Mx[:meta_o]}(date)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 7
223 when /^#{Mx[:meta_o]}(date\.created)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
224 when /^#{Mx[:meta_o]}(date\.issued)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
225 when /^#{Mx[:meta_o]}(date\.available)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
226 when /^#{Mx[:meta_o]}(date\.valid)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
227 when /^#{Mx[:meta_o]}(date\.modified)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
228 when /^#{Mx[:meta_o]}(type)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 8
229 when /^#{Mx[:meta_o]}(format)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 9
230 when /^#{Mx[:meta_o]}(identifier)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 10
231 when /^#{Mx[:meta_o]}(source)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 11
232 when /^#{Mx[:meta_o]}(language)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 12
233 when /^#{Mx[:meta_o]}(relation)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 13
234 when /^#{Mx[:meta_o]}(coverage)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 14
235 when /^#{Mx[:meta_o]}(rights)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','dc') #dc 15
236 when /^#{Mx[:meta_o]}(keywords)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
237 when /^#{Mx[:meta_o]}(copyright)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
238 when /^#{Mx[:meta_o]}(translator|translated_by)#{Mx[:meta_c]}\s*(.+?)$/ then header('translator',$2)
239 when /^#{Mx[:meta_o]}(illustrator|illustrated_by)#{Mx[:meta_c]}\s*(.+?)$/ then header('illustrator',$2)
240 when /^#{Mx[:meta_o]}(prepared_by)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
241 when /^#{Mx[:meta_o]}(digitized_by)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
242 when /^#{Mx[:meta_o]}(comments?)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
243 when /^#{Mx[:meta_o]}(abstract)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
244 when /^#{Mx[:meta_o]}(tags?)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
245 when /^#{Mx[:meta_o]}(catalogue)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'meta','extra')
246 when /^#{Mx[:meta_o]}(class(?:ify)?_loc)#{Mx[:meta_c]}\s*(.+?)$/ then header('classify_loc',$2,'meta','extra')
247 when /^#{Mx[:meta_o]}(class(?:ify)?_dewey)#{Mx[:meta_c]}\s*(.+?)$/ then header('classify_dewey',$2,'meta','extra')
248 when /^#{Mx[:meta_o]}(class(?:ify)?_pg)#{Mx[:meta_c]}\s*(.+?)$/ then header('classify_pg',$2,'meta','extra')
249 when /^#{Mx[:meta_o]}(class(?:ify)?_isbn)#{Mx[:meta_c]}\s*(.+?)$/ then header('classify_isbn',$2,'meta','extra')
250 when /^#{Mx[:meta_o]}(toc|structure)#{Mx[:meta_c]}\s*(.+?)$/ then header('structure',$2,'process','instruct')
251 when /^#{Mx[:meta_o]}(level|page|markup)#{Mx[:meta_c]}\s*(.+?)$/ then header('markup',$2,'process','instruct')
252 when /^#{Mx[:meta_o]}(bold)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct')
253 when /^#{Mx[:meta_o]}(italics|itali[sz]e)#{Mx[:meta_c]}\s*(.+?)$/ then header('italicize',$2,'process','instruct')
254 when /^#{Mx[:meta_o]}(vocabulary|wordlist)#{Mx[:meta_c]}\s*(.+?)$/ then header('vocabulary',$2,'process','instruct')
255 when /^#{Mx[:meta_o]}(css|stylesheet)#{Mx[:meta_c]}\s*(.+?)$/ then header('css',$2,'process','instruct')
256 when /^#{Mx[:meta_o]}(links)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct')
257 when /^#{Mx[:meta_o]}(prefix)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct') #add a & b
258 when /^#{Mx[:meta_o]}(suffix)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct')
259 when /^#{Mx[:meta_o]}(information)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct')
260 when /^#{Mx[:meta_o]}(contact)#{Mx[:meta_c]}\s*(.+?)$/ then header($1,$2,'process','instruct')
261 when /^#{Mx[:meta_o]}(rcs|cvs)#{Mx[:meta_c]}\s*(.+?)$/ then header('version',$2,'process','instruct')
262 else nil
263 end
264 end
265 def dublin
266 (@p =~/^#{Mx[:meta_o]}\S+?#{Mx[:meta_c]}/) \
267 ? start_is_match
268 : nil
269 end
270 def meta
271 (@p =~/^#{Mx[:meta_o]}\S+?#{Mx[:meta_c]}/) \
272 ? start_is_match
273 : nil
274 end
275 end
276 class ParagraphNumber
277 def initialize(paranum)
278 @paranum=/(\d+)/m.match(paranum)[1]
279 end
280 def display
281 @paranum.gsub(/(\d+)/,'#\1')
282 end
283 end
284 class PotNumber
285 @@n=0
286 def initialize
287 @@n +=2
288 end
289 def num
290 @@n
291 end
292 def reset
293 @@n=0
294 end
295 end
296 end
297 __END__