aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v3/odf.rb
diff options
context:
space:
mode:
authorRalph Amissah <ralph@amissah.com>2012-12-12 14:58:58 -0500
committerRalph Amissah <ralph@amissah.com>2012-12-12 14:58:58 -0500
commit1beb5aa3c376dbef89bc33024216650529ef7262 (patch)
tree1682500089c14fd49bcf52a3a001e291fadd5f75 /lib/sisu/v3/odf.rb
parentdebian/changelog (3.3.2-1) (diff)
parentv3: 3.3.3 version & changelog, dates touched (diff)
Merge tag 'sisu_3.3.3' into debian/sid
Diffstat (limited to 'lib/sisu/v3/odf.rb')
-rw-r--r--lib/sisu/v3/odf.rb182
1 files changed, 99 insertions, 83 deletions
diff --git a/lib/sisu/v3/odf.rb b/lib/sisu/v3/odf.rb
index 4985b3dd..807d2832 100644
--- a/lib/sisu/v3/odf.rb
+++ b/lib/sisu/v3/odf.rb
@@ -101,7 +101,7 @@ module SiSU_ODF
require_relative 'defaults' # defaults.rb
require_relative 'shared_txt' # shared_txt.rb
@@img_count=0
- @@odf={ body: [], open: [], close: [], head: [], metadata: [], tail: [], endnotes: [] }
+ @@odf={ body: [], head: [], toc: [], metadata: [], tail: [], book_idx: [], endnotes: [] }
@@docstart=true
@@fns=nil
def initialize(particulars)
@@ -137,13 +137,26 @@ module SiSU_ODF
if n =~/#{Mx[:br_line]}/
fix=n.split(/#{Mx[:br_line]}/) #watch #added
fix.each do |x|
- if x =~/\S+/; @n << x
+ if x =~/\S+/ then @n << x
end
end
else @n << n
end
end
end
+ def odf_book_idx
+ if @md.book_idx
+ idx_arr,idx_raw=[],SiSU_Particulars::CombinedSingleton.instance.get_idx_raw(@md.opt).raw_idx
+ idx_raw.each do |x|
+ x=if x.is_a?(String)
+ SiSU_ODF_Format::FormatBookIndex.new(x).book_idx_bookmark
+ else nil
+ end
+ idx_arr << x.strip if x.is_a?(String)
+ end
+ @@odf[:book_idx]=idx_arr.join
+ end
+ end
def odf_metadata
@@odf[:metadata]=SiSU_Metadata::Summary.new(@md).odf.metadata
end
@@ -162,8 +175,10 @@ module SiSU_ODF
@@odf[:tail] << %{\n<text:p text:style-name="P_normal">SiSU: &lt;<text:a xlink:type="simple" xlink:href="http://www.jus.uio.no/lm">www.jus.uio.no/sisu</text:a>&gt; and &lt;<text:a xlink:type="simple" xlink:href="http://www.sisudoc.org">www.sisudoc.org</text:a>&gt;</text:p>}
@@odf[:tail] << "\n</office:text></office:body></office:document-content>"
end
- def heading(dob,p_num='')
- dob.obj=dob.obj.gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,'') #check
+ def set_bookmark_tag(dob)
+ tags=SiSU_ODF_Format::Tags.new.set_bookmark_tag(dob)
+ end
+ def heading(dob,p_num)
m=/#{$1}/
breakpage=''
if @md.fns \
@@ -180,7 +195,19 @@ module SiSU_ODF
end
end
@@docstart=false
- dob.obj=%{#{breakpage}<text:h text:style-name="H_#{dob.ln}" text:outline-level="#{dob.ln}">#{dob.obj}#{p_num}</text:h>}
+ dob.tmp=dob.obj
+ dob.obj=%{#{breakpage}<text:h text:style-name="H_#{dob.ln}" text:outline-level="#{dob.ln}">#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}</text:h>}
+ dob
+ end
+ def toc(dob,p_num)
+ m=/#{$1}/
+ hardspace=(dob.lv =~/[A-C]/i) \
+ ? '<text:p text:style-name="Standard"/>'
+ : ''
+ toc_heading=dob.ocn \
+ ? %{<text:bookmark-ref text:reference-format="text" text:ref-name="#{dob.ocn}">#{dob.tmp}</text:bookmark-ref>}
+ : dob.tmp
+ dob.obj=%{<text:h text:style-name="H_#{dob.ln}" text:outline-level="#{dob.ln}">#{toc_heading}</text:h>#{hardspace}}
dob
end
def image_src(i)
@@ -244,7 +271,7 @@ module SiSU_ODF
dob.obj.scan(/(#{Mx[:lnk_o]}[ ]*(.+?)[ ]*#{Mx[:lnk_c]}(image))/)
else nil
end
- if m; m.each do |i|
+ if m then m.each do |i|
cont,url=i[1],i[2]
cont=cont.gsub(/([)(\]\[])/,"\\\\\\1").
gsub(/([+?])/,"\\\\\\1") # incorrect handling of +
@@ -282,6 +309,10 @@ module SiSU_ODF
end
t
end
+ def text_link_odf_bookmark(txt,url,trail)
+ map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map
+ %{<text:bookmark-ref text:reference-format="text" text:ref-name="#{url}">#{txt.strip}</text:bookmark-ref>#{trail}}
+ end
def text_link(dob)
m=dob.obj.scan(/(#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]})/) #sort
if m
@@ -305,6 +336,19 @@ module SiSU_ODF
txt=txt.gsub(/([)(\]\[])/,"\\\\\\1").
gsub(/([+?*])/,"\\\\\\1") # problems with +
url=url.gsub(/([+?])/,"\\\\\\1") # problems with +
+ dob.obj=dob.obj.gsub(/#{Mx[:lnk_o]}[ ]*#{txt}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{url}#{Mx[:rel_c]}/m,text_link_odf_bookmark(txt,url,trail)). #make sure trailing ']' are not caught in url
+ gsub(/\\([)(\]\[?])/,'\1') #clumsy fix
+ end
+ m=nil
+ end
+ dob
+ end
+ def text_link_relative_(dob)
+ m=dob.obj.scan(/(#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]})/) #sort
+ if m
+ m.each do |i|
+ txt,url,trail=i[1],i[2]
+ txt=txt.gsub(/([)(\]\[])/,"\\\\\\1")
dob.obj=dob.obj.gsub(/#{Mx[:lnk_o]}[ ]*#{txt}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{url}#{Mx[:rel_c]}/m,text_link_odf(txt,url,trail)). #make sure trailing ']' are not caught in url
gsub(/\\([)(\]\[?])/,'\1') #clumsy fix
end
@@ -312,7 +356,7 @@ module SiSU_ODF
end
dob
end
- def normal(dob,p_num='') #P1 - P3
+ def normal(dob,p_num) #P1 - P3
dob.obj=dob.obj.gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,
'<text:a xlink:type="simple" xlink:href="\1">\1</text:a>'). #http ftp matches escaped, no decoration
gsub(/#{Mx[:url_o]}([a-zA-Z0-9._-]+\@\S+?\.[a-zA-Z0-9._-]+)#{Mx[:url_c]}/,
@@ -322,12 +366,12 @@ module SiSU_ODF
dob.obj= if dob.is==:para \
and dob.indent.to_s =~/[0-9]/ \
and dob.indent == dob.hang
- %{<text:p text:style-name="P_indent_#{dob.indent}">#{dob.obj}#{p_num}</text:p>}
+ %{<text:p text:style-name="P_indent_#{dob.indent}">#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}</text:p>}
elsif dob.is==:para \
and dob.hang.to_s =~/[0-9]/ \
and dob.indent != dob.hang
- %{<text:p text:style-name="P_h#{dob.hang}_i#{dob.indent}">#{dob.obj}#{p_num}</text:p>}
- else %{<text:p text:style-name="P_normal">#{dob.obj}#{p_num}</text:p>}
+ %{<text:p text:style-name="P_h#{dob.hang}_i#{dob.indent}">#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}</text:p>}
+ else %{<text:p text:style-name="P_normal">#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}</text:p>}
end
dob
end
@@ -341,8 +385,8 @@ module SiSU_ODF
str
end
def footnote(t_o)
- str=if defined? t_o.obj; t_o.obj
- elsif t_o.class==String; t_o
+ str=if defined? t_o.obj then t_o.obj
+ elsif t_o.is_a?(String) then t_o
end
if str
@astx||=10000
@@ -376,8 +420,8 @@ module SiSU_ODF
end
end
end
- if defined? t_o.obj; t_o.obj=str
- elsif t_o.class==String; t_o=str
+ if defined? t_o.obj then t_o.obj=str
+ elsif t_o.is_a?(String) then t_o=str
end
t_o
end
@@ -390,12 +434,13 @@ module SiSU_ODF
end
def poem(dob,p_num) #P4 #same as group
parray=[]
- dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock|
+ dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i|
+ set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : ''
parablock=group_clean(parablock)
- parray << %{<text:p text:style-name="P_group">#{parablock}</text:p>} if parablock =~/\S+/
+ parray << %{<text:p text:style-name="P_group">#{set_ref}#{parablock}</text:p>} if parablock =~/\S+/
end
dob.obj=parray.join \
- + %{<text:p text:style-name="P_group">#{p_num}</text:p>} \
+ + %{<text:p text:style-name="P_group">#{p_num[:display]}</text:p>} \
+ '<text:p text:style-name="Standard"/>'
dob
end
@@ -407,7 +452,8 @@ module SiSU_ODF
%{#{@brace_url.xml_open}<text:a xlink:type="simple" xlink:href="mailto:\\1">\\1</text:a>#{@brace_url.xml_close}}).
gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,
%{#{@brace_url.xml_open}<text:a xlink:type="simple" xlink:href="\\1">\\1</text:a>#{@brace_url.xml_close}}) #http ftp matches with decoration
- dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock|
+ dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i|
+ set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : ''
parablock=group_clean(parablock)
parablock=parablock.gsub(/&lt;text:a xlink:type="simple" xlink:href="(.+?)"&gt;/m,'<text:a xlink:type="simple" xlink:href="\1">').
gsub(/&lt;(\/text:a)&gt;/,'<\1>').
@@ -415,10 +461,10 @@ module SiSU_ODF
gsub(/&lt;(text:p text:style-name="Footnote")&gt;/,'<\1>').
gsub(/&lt;(\/?text:(?:note-citation|note-body|note|p))&gt;/,'<\1>')
parablock=footnote(parablock)
- parray << %{<text:p text:style-name="P_group">#{parablock}</text:p>} if parablock =~/\S+/
+ parray << %{<text:p text:style-name="P_group">#{set_ref}#{parablock}</text:p>} if parablock =~/\S+/
end
dob.obj=parray.join \
- + %{<text:p text:style-name="P_group">#{p_num}</text:p>} \
+ + %{<text:p text:style-name="P_group">#{p_num[:display]}</text:p>} \
+ '<text:p text:style-name="Standard"/>'
dob
end
@@ -430,7 +476,8 @@ module SiSU_ODF
%{#{@brace_url.xml_open}<text:a xlink:type="simple" xlink:href="mailto:\\1">\\1</text:a>#{@brace_url.xml_close}}).
gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,
%{#{@brace_url.xml_open}<text:a xlink:type="simple" xlink:href="\\1">\\1</text:a>#{@brace_url.xml_close}}) #http ftp matches with decoration
- dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock|
+ dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i|
+ set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : ''
parablock=group_clean(parablock)
parablock=parablock.gsub(/&lt;text:a xlink:type="simple" xlink:href="(.+?)"&gt;/m,'<text:a xlink:type="simple" xlink:href="\1">').
gsub(/&lt;(\/text:a)&gt;/,'<\1>').
@@ -438,10 +485,10 @@ module SiSU_ODF
gsub(/&lt;(text:p text:style-name="Footnote")&gt;/,'<\1>').
gsub(/&lt;(\/?text:(?:note-citation|note-body|note|p))&gt;/,'<\1>')
parablock=footnote(parablock)
- parray << %{<text:p text:style-name="P_group">#{parablock}</text:p>} if parablock =~/\S+/
+ parray << %{<text:p text:style-name="P_group">#{set_ref}#{parablock}</text:p>} if parablock =~/\S+/
end
dob.obj=parray.join \
- + %{<text:p text:style-name="P_group">#{p_num}</text:p>} \
+ + %{<text:p text:style-name="P_group">#{p_num[:display]}</text:p>} \
+ '<text:p text:style-name="Standard"/>'
dob
end
@@ -449,15 +496,16 @@ module SiSU_ODF
if dob.is==:code
dob.obj=dob.obj.gsub(/\s\s/,'&#160;&#160;')
parray=[]
- dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock|
+ dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i|
+ set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : ''
parablock=group_clean(parablock)
parablock=parablock.gsub(/^\s*$/,'<br />').
gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,
'<text:a xlink:type="simple" xlink:href="\1">\1</text:a>') #http ftp matches escaped, no decoration
- parray << %{<text:p text:style-name="P_code">#{parablock}</text:p>} if parablock =~/\S+/
+ parray << %{<text:p text:style-name="P_code">#{set_ref}#{parablock}</text:p>} if parablock =~/\S+/
end
dob.obj=parray.join \
- + %{<text:p text:style-name="P_group">#{p_num}</text:p>} \
+ + %{<text:p text:style-name="P_group">#{p_num[:display]}</text:p>} \
+ '<text:p text:style-name="Standard"/>'
end
dob
@@ -490,18 +538,22 @@ module SiSU_ODF
dob
else dob
end
- p_num=''
- if @env.odt_ocn?
- if dob.is !~/(^#{Rx[:meta]}|#{Mx[:br_eof]}|#{Mx[:br_endnotes]})/
- if defined? dob.ocn \
- and not dob.ocn.nil?
- p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).display
- end
+ p_num={ display: '', set_ref: '' }
+ if dob.is !~/(^#{Rx[:meta]}|#{Mx[:br_eof]}|#{Mx[:br_endnotes]})/
+ if defined? dob.ocn \
+ and dob.ocn.is_a?(Fixnum)
+ p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).set_bookmark_and_display
+ #p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).set_ref_and_display
end
end
dob=footnote(dob)
if dob.is==:heading
@@odf[:body] << heading(dob,p_num).obj << @br*2
+ if SiSU_Env::ProcessingSettings.new(md).build.toc?
+ if dob.lv =~/[A-C1]/i
+ @@odf[:toc] << toc(dob,p_num).obj
+ end
+ end
elsif dob.is ==:verse
@@odf[:body] << poem(dob,p_num).obj << @br*2
elsif dob.is==:group
@@ -539,6 +591,7 @@ module SiSU_ODF
else STDERR.puts %{\t*WARN* did not find image - "#{bullet}/bullet_09.png" [#{__FILE__}:#{__LINE__}]}
end
end
+ odf_book_idx
odf_metadata
data.each do |dob|
#p dob.obj if dob.obj =~safe_characters and @md.opt.cmd =~/V/ #KEEP
@@ -573,7 +626,7 @@ module SiSU_ODF
dob.obj=dob.obj.gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'<del>\1</del>').
gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'<ins>\1</ins>').
gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'<cite>\1</cite>').
- gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,''). #check
+ gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,'<text:bookmark-start text:name="\1"/><text:bookmark-end text:name="\1"/>'). #check
gsub(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;').
gsub(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;').
gsub(/#{Mx[:mk_o]}[~-]##{Mx[:mk_c]}/,'')
@@ -693,14 +746,19 @@ WOK
def publish
divider='='
content=[]
+ br_pg='<text:p text:style-name="P_normal_page_new"> </text:p>'
data=@data
- content << @@odf[:open]
- content << @@odf[:head]
- content << @@odf[:body]
- content << @@odf[:metadata]
- content << @@odf[:tail]
+ content <<
+ @@odf[:head] <<
+ @@odf[:toc] <<
+ br_pg <<
+ @@odf[:body] <<
+ @@odf[:book_idx] <<
+ br_pg <<
+ @@odf[:metadata] <<
+ @@odf[:tail]
SiSU_ODF::Source::Output.new(content,@md,@env).odf
- @@odf[:head],@@odf[:body],@@odf[:tail],@@odf[:metadata]=[],[],[],[]
+ @@odf={ head: [], toc: [], body: [], tail: [], book_idx: [], metadata: [] }
end
end
class Output <Source
@@ -739,7 +797,7 @@ WOK
env.make_path(@md.file.output_path.odt.dir)
filename="#{@env.processing_path.odt}/content.xml"
od=File.new(filename,'w+')
- @content.each do |para| # this is a hack
+ @content.compact.each do |para| # this is a hack
od.puts para unless para =~/\A\s*\Z/
end
od.close
@@ -759,45 +817,3 @@ WOK
end
end
__END__
-todo:
-* table of contents
-* page header/footer?
-
-done:
-headings *
-footnotes *
-bold underscore italics strikethrough *
-superscript subscript *
-extended ascii set *
-indents *
- [autonomy_markup0.sst] *
-groups
- poem *
- code *
- tables
-images
-bullet
-line break
-page break
-
-notes?? [you could add a note number for every object/paragraph!]
-
- FileUtils::cd(@env.processing_path.odt)
- structure=[]
- Find.find(@env.processing_path.odt) do |f|
- structure << puts f
- end
- open(opendoc,'wb') do |f|
- zip=Zlib::ZipWriter.new(f)
- structure.each |z| do
- zip << z
- end
- zip.close
- end
- #zip -qr #{opendoc} *
- FileUtils::mv(opendoc,"#{@env.path.output}/#{@md.fnb}/.")
- FileUtils::cd(Dir.pwd)
-●
-<text:span text:style-name="T6">●</text:span> #bullet
-dob.obj.gsub!(/^(#{Mx[:pa_o]}:i[1-9]#{Mx[:pa_c]})\s*#{Mx[:gl_bullet]}/,'\1 <text:span text:style-name="T6">·</text:span> ') #bullet
-dob.obj.gsub!(/^#{Mx[:gl_bullet]}/,'<text:span text:style-name="T6">·</text:span> ') #bullet