From fec639c59a032977b87ff49920e74ae08d485f04 Mon Sep 17 00:00:00 2001
From: Ralph Amissah <ralph@amissah.com>
Date: Wed, 26 May 2010 13:18:31 -0400
Subject: output formats internal document (and relative) linking as possible

---
 lib/sisu/v2/concordance.rb      |  2 +-
 lib/sisu/v2/epub_segments.rb    | 18 +++++++++----
 lib/sisu/v2/epub_tune.rb        | 16 +++++++++++-
 lib/sisu/v2/html_scroll.rb      |  1 +
 lib/sisu/v2/html_segments.rb    | 14 ++++++++---
 lib/sisu/v2/html_tune.rb        |  5 ++++
 lib/sisu/v2/odf.rb              | 56 +++++++++++++++++++++++++++++++++++++----
 lib/sisu/v2/particulars.rb      |  3 ++-
 lib/sisu/v2/plaintext.rb        |  1 +
 lib/sisu/v2/shared_html_lite.rb | 29 +++++++++++++++++----
 lib/sisu/v2/shared_xml.rb       |  2 ++
 lib/sisu/v2/texpdf_format.rb    | 56 +++++++++++++++++++++++------------------
 lib/sisu/v2/webrick.rb          |  4 ++-
 13 files changed, 160 insertions(+), 47 deletions(-)

diff --git a/lib/sisu/v2/concordance.rb b/lib/sisu/v2/concordance.rb
index d8da2005..03635cd0 100644
--- a/lib/sisu/v2/concordance.rb
+++ b/lib/sisu/v2/concordance.rb
@@ -191,7 +191,7 @@ WOK
           @rxp_t1=Regexp.new('^T1')
           @rxp_t2=Regexp.new('^T2')
           @rxp_t3=Regexp.new('^T3')
-          @rxp_excluded1=/#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
+          @rxp_excluded1=/#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
           @rxp_excluded0=/^(?:#{Mx[:fa_bold_o]}|#{Mx[:fa_italics_o]})?(?:to\d+|\d+|&nbsp;|#{Mx[:br_endnotes]}|EOF|#{Mx[:br_eof]}|thumb_\S+|snap_\S+|_+|-+|[(]?(?:ii+|iv|vi+|ix|xi+|xiv|xv|xvi+|xix|xx)[).]?|\S+?_\S+|[\d_]+\w\S+|[\w\d]{1,2}|\d{1,3}\w?|#{@dp}|[0-9a-f]{16,64}|\d{2,3}x\d{2,3}|\S{0,2}sha\d|\S{0,3}\d{4}w\d\d|\b\w\d+|\d_all\b|e\.?g\.?)(?:#{Mx[:fa_bold_c]}|#{Mx[:fa_italics_c]})?$/mi #this regex causes and cures a stack dump in ruby 1.9 !!!
           @rgx_splitlist=%r{[—.,;:#{Mx[:nbsp]}-]}mi
           @rgx_scanlist=%r{#{Mx[:fa_italics_o]}[a-zA-Z0-9"\s]{2,12}#{Mx[:fa_italics_c]}|#{Mx[:fa_bold_o]}[a-zA-Z0-9"\s]{2,12}#{Mx[:fa_bold_c]}|(?:https?|file)://\S+|<\S+?>|\w+|[a-zA-Z]+}mi
diff --git a/lib/sisu/v2/epub_segments.rb b/lib/sisu/v2/epub_segments.rb
index 35edf971..66d73f4f 100644
--- a/lib/sisu/v2/epub_segments.rb
+++ b/lib/sisu/v2/epub_segments.rb
@@ -181,11 +181,19 @@ WOK
       SiSU_Screen::Ansi.new(@md.cmd,@@seg_name.length) if @md.cmd =~/[MVv]/
       map_nametags=SiSU_Particulars::Combined_singleton.instance.get_map_nametags(@md).nametags_map #p map_nametags
       data.each do |dob|
-        if defined? dob.obj \
-        and dob.obj =~/href="#{Xx[:segment]}#(\S+?)"/
-          m=$1
-          dob.obj.gsub!(/href="#{Xx[:segment]}#(\S+?)"/,%{href="#{map_nametags[m][:segname]}#{Sfx[:html]}#\\1"})
-        end
+        #if defined? dob.obj \
+        #and dob.obj =~/href="#{Xx[:segment]}#+\S+?"/
+        #  ##Consider: remove, reinstate earlier?
+        #  #while dob.obj =~/href="#{Xx[:segment]}#+(\S+?)"/
+        #  #  m=$1
+        #  #  if map_nametags[m][:segname]
+        #  #    dob.obj.sub!(/href="#{Xx[:segment]}#+(\S+?)"/,%{href="#{map_nametags[m][:segname]}#{Sfx[:html]}#\\1"})
+        #  #  else
+        #  #    p "NOT FOUND name_tags: #{m}"
+        #  #    dob.obj.sub!(/href="#{Xx[:segment]}#+(\S+?)"/,%{href="#\\1"}) # not satisfactory
+        #  #  end
+        #  #end
+        #end
         if (dob.is=='heading' or dob.is=='heading_insert') \
         and dob.ln==4
           if dob.ocn==0
diff --git a/lib/sisu/v2/epub_tune.rb b/lib/sisu/v2/epub_tune.rb
index d49ac3c1..9115a14d 100644
--- a/lib/sisu/v2/epub_tune.rb
+++ b/lib/sisu/v2/epub_tune.rb
@@ -285,7 +285,21 @@ module SiSU_EPUB_Tune
             link=m[/(.+)/m]
             png=m.scan(/\S+/)[0].strip
             link=link.strip
-            u.gsub!(/(\S+)/,"#{map_nametags[u][:segname]}#{Sfx[:xhtml]}#\\1") if u !~/\// unless http_
+            u.sub!(/^#*/,'') #make neater
+            if map_nametags[u] \
+            and map_nametags[u][:segname]
+              if u=~/^\d+$/
+                u.gsub!(/^(\d+)$/,"#{map_nametags[u][:segname]}#{Sfx[:xhtml]}#o\\1") if u !~/\// unless http_
+              else
+                u.gsub!(/(\S+)/,"#{map_nametags[u][:segname]}#{Sfx[:xhtml]}#\\1") if u !~/\// unless http_
+              end
+            elsif u =~/^:/
+              u.gsub!(/^:/,"#{@env.url.root}/")
+            elsif u =~/^\.\.\//
+              u.gsub!(/^\.\.\//,"#{@env.url.root}/")
+            elsif u =~/http:\/\//
+            else p "NOT FOUND name_tags: #{u}"
+            end
             ins=%{<a href="#{u}">#{link}</a>}
             word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,ins)
             word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,ins)
diff --git a/lib/sisu/v2/html_scroll.rb b/lib/sisu/v2/html_scroll.rb
index a5ff9b00..b0581ead 100644
--- a/lib/sisu/v2/html_scroll.rb
+++ b/lib/sisu/v2/html_scroll.rb
@@ -94,6 +94,7 @@ module SiSU_HTML_scroll
         and dob.obj =~/Document Information/ #fix no current match
           @rcdc=true
         end
+        dob.obj.gsub!(/href="[a-z0-9._-]+(#\S+?")/m,'href="\1') # internal document links
         dob.obj.gsub!(/href="#{Xx[:segment]}/m,'href="')
         dob.obj.gsub!(/(?:\s*#{Mx[:br_page]}\s*|\s*#{Mx[:br_page_new]}\s*)+/m,'<p><hr width=90% /></p>')
         if dob.obj !~/(^#{Rx[:meta]}|#{Mx[:br_eof]})/
diff --git a/lib/sisu/v2/html_segments.rb b/lib/sisu/v2/html_segments.rb
index 83f9c5c9..bed30afa 100644
--- a/lib/sisu/v2/html_segments.rb
+++ b/lib/sisu/v2/html_segments.rb
@@ -166,9 +166,17 @@ module SiSU_HTML_seg
       map_nametags=SiSU_Particulars::Combined_singleton.instance.get_map_nametags(@md).nametags_map #p map_nametags
       data.each do |dob|
         if defined? dob.obj \
-        and dob.obj =~/href="#{Xx[:segment]}#(\S+?)"/
-          m=$1
-          dob.obj.gsub!(/href="#{Xx[:segment]}#(\S+?)"/,%{href="#{map_nametags[m][:segname]}#{Sfx[:html]}#\\1"})
+        and dob.obj =~/href="#{Xx[:segment]}#+\S+?"/
+          while dob.obj =~/href="#{Xx[:segment]}#+(\S+?)"/
+            m=$1
+            if map_nametags[m] \
+            and map_nametags[m][:segname]
+              dob.obj.sub!(/href="#{Xx[:segment]}#+(\S+?)"/,%{href="#{map_nametags[m][:segname]}#{Sfx[:html]}#\\1"})
+            else
+              p "NOT FOUND name_tags: #{m}"
+              dob.obj.sub!(/href="#{Xx[:segment]}#+(\S+?)"/,%{href="#\\1"}) # not satisfactory
+            end
+          end
         end
         if (dob.is=='heading' or dob.is=='heading_insert') \
         and dob.ln==4
diff --git a/lib/sisu/v2/html_tune.rb b/lib/sisu/v2/html_tune.rb
index c0efb72e..11b1dbde 100644
--- a/lib/sisu/v2/html_tune.rb
+++ b/lib/sisu/v2/html_tune.rb
@@ -255,6 +255,10 @@ module SiSU_HTML_Tune
           http_=true
           if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
             m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/.match(word).captures
+          elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}:\S+?#{Mx[:rel_c]}/
+            #http_=false
+            m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}:(\S+?)#{Mx[:rel_c]}/.match(word).captures
+            u='../' + u
           elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
             http_=false
             m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/.match(word).captures
@@ -326,6 +330,7 @@ module SiSU_HTML_Tune
           and dob.obj !~/(\"..\/\S+?\"|>\s*..\/\S+<)/
             dob.obj.gsub!(/(\.\.\/\S+)/,'<a href="\1">\1</a>')
           end
+          dob.obj.gsub!(/<a href=":/,%{<a href="#{@vz.url_site}/})
           dob.obj.gsub!(/<a href="\.\.\//,%{<a href="#{@vz.url_site}/})
         else
           dob.obj.gsub!(/(^|[^}])_</m,'\1&lt;'); dob.obj.gsub!(/(^|[^}])_>/m,'\1&gt;') #code-block: angle brackets special characters
diff --git a/lib/sisu/v2/odf.rb b/lib/sisu/v2/odf.rb
index dc8c90a8..24f45f50 100644
--- a/lib/sisu/v2/odf.rb
+++ b/lib/sisu/v2/odf.rb
@@ -236,7 +236,27 @@ module SiSU_ODF
       def text_link_odf(txt,url,trail)
         txt.gsub!(/(\\\+)/,'+') #this is convoluted, and risky :-(
         url.gsub!(/(\\\+)/,'+') #this is convoluted, and risky :-(
-        %{<text:a xlink:type="simple" xlink:href="#{url}">#{txt.strip}</text:a>#{trail}}
+        map_nametags=SiSU_Particulars::Combined_singleton.instance.get_map_nametags(@md).nametags_map 
+        t=case url
+        when /^http:/
+          %{<text:a xlink:type="simple" xlink:href="#{url}">#{txt.strip}</text:a>#{trail}}
+        when /^:/                 # site same document collection html link
+          url.gsub!(/^:/,"#{@env.url.root}/")
+          %{<text:a xlink:type="simple" xlink:href="#{url}">#{txt.strip}</text:a>#{trail}}
+        when /^\.\.\//                 # site same document collection html link
+          url.gsub!(/^\.\.\//,"#{@env.url.root}/")
+          %{<text:a xlink:type="simple" xlink:href="#{url}">#{txt.strip}</text:a>#{trail}}
+        else                           # document internal link
+          if map_nametags[url] \
+          and map_nametags[url][:segname]
+          else p "NOT FOUND name_tags: #{url}"
+          end
+          t=map_nametags[url] \
+          && map_nametags[url][:segname] \
+          ? %{<text:a xlink:type="simple" xlink:href="#{@env.url.root}/#{@md.fnb}/#{map_nametags[url][:segname]}#{Sfx[:html]}##{url}">#{txt.strip}</text:a>#{trail}} \
+          : %{#{txt.strip}#{trail}}
+        end
+        t
       end
       def text_link(dob)
         m=dob.obj.scan(/(#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]})/) #sort
@@ -253,6 +273,21 @@ module SiSU_ODF
         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.gsub!(/([)(\]\[])/,"\\\\\\1")
+            txt.gsub!(/([+?*])/,"\\\\\\1") # problems with +
+            url.gsub!(/([+?])/,"\\\\\\1") # problems with +
+            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
+            dob.obj.gsub!(/\\([)(\]\[?])/,'\1') #clumsy fix
+          end
+          m=nil
+        end
+        dob
+      end
       def normal(dob)                                                           #P1 - P3
         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
@@ -268,6 +303,13 @@ module SiSU_ODF
       end
       def fontface(dob)
       end
+      def footnote_urls(str)
+        str.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}})
+        str=text_link(str) if str =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
+        str=text_link_relative(str) if str =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
+        str
+      end
       def footnote(t_o)
         str=if defined? t_o.obj; t_o.obj
         elsif t_o.class==String; t_o
@@ -281,6 +323,8 @@ module SiSU_ODF
             asterisk=str.scan(/#{Mx[:en_a_o]}([*+]+)\s+(.+?)#{Mx[:en_a_c]}/)
             asterisk.each do |x|
               a=x[0].gsub(/([*+])/,"\\\\\\1")
+              str=group_clean(str)
+              str=footnote_urls(str)
               str.gsub!(/#{Mx[:en_a_o]}(#{a})\s+(.+?)#{Mx[:en_a_c]}/,%{<text:note text:id="ftn#{@astx.to_s}" text:note-class="footnote"><text:note-citation text:label="\\1">\\1</text:note-citation><text:note-body><text:p text:style-name="Footnote"> \\2</text:p><text:p text:style-name="Footnote"/></text:note-body></text:note>})
               @astx+=1
             end
@@ -289,6 +333,8 @@ module SiSU_ODF
             asterisk=str.scan(/#{Mx[:en_b_o]}([*+]\d+)\s+(.+?)#{Mx[:en_b_c]}/)
             asterisk.each do |x|
               a=x[0].gsub(/([*+])/,"\\\\\\1")
+              str=group_clean(str)
+              str=footnote_urls(str)
               str.gsub!(/#{Mx[:en_b_o]}(#{a})\s+(.+?)#{Mx[:en_b_c]}/,%{<text:note text:id="ftn#{@astx.to_s}" text:note-class="footnote"><text:note-citation text:label="\\1">\\1</text:note-citation><text:note-body><text:p text:style-name="Footnote"> \\2</text:p><text:p text:style-name="Footnote"/></text:note-body></text:note>})
               @astx+=1
             end
@@ -362,10 +408,10 @@ module SiSU_ODF
       def odf_structure(md,dob)
         @md,@dob=md,dob
         dob=if dob.is !='code'
-          dob=if dob.obj =~/#{Mx[:lnk_o]}[ ]*\S+?\.(?:png|jpg|gif)\s.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/; image(dob)
-          elsif dob.obj =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/; text_link(dob)
-          else dob
-          end
+          dob=image(dob) if dob.obj =~/#{Mx[:lnk_o]}[ ]*\S+?\.(?:png|jpg|gif)\s.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/
+          dob=text_link(dob) if dob.obj =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
+          dob=text_link_relative(dob) if dob.obj =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
+          dob
         else dob
         end
         dob=footnote(dob)
diff --git a/lib/sisu/v2/particulars.rb b/lib/sisu/v2/particulars.rb
index 96c4fbac..b2f533e1 100644
--- a/lib/sisu/v2/particulars.rb
+++ b/lib/sisu/v2/particulars.rb
@@ -177,7 +177,8 @@ module SiSU_Particulars
     attr_accessor :nametags_map
     def set_nametags_map
       begin
-        @nametags_map=SiSU_DAL::Source.new(@md).get_map_nametags
+        opt=@md ? @md : @opt
+        @nametags_map=SiSU_DAL::Source.new(opt).get_map_nametags
         self
       rescue; SiSU_Errors::Info_error.new($!,$@,@opt.cmd,@opt.fns).error
       end
diff --git a/lib/sisu/v2/plaintext.rb b/lib/sisu/v2/plaintext.rb
index a57339f4..4acdec93 100644
--- a/lib/sisu/v2/plaintext.rb
+++ b/lib/sisu/v2/plaintext.rb
@@ -299,6 +299,7 @@ WOK
           dob.obj.gsub!(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,
             "#{Px[:strike_o]}\\1#{Px[:strike_c]}")
           unless dob.is=='code'
+            dob.obj.gsub!(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,'\1')
             dob.obj.gsub!(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'\1')
             dob.obj.gsub!(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,'\1 [link: <\2>]')
             dob.obj.gsub!(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}image/,'\1 [link: local image]')
diff --git a/lib/sisu/v2/shared_html_lite.rb b/lib/sisu/v2/shared_html_lite.rb
index 6303691b..c1f53e10 100644
--- a/lib/sisu/v2/shared_html_lite.rb
+++ b/lib/sisu/v2/shared_html_lite.rb
@@ -62,6 +62,7 @@ module SiSU_Format_Shared
     include SiSU_Viz
   class CSS_Format
     require "#{SiSU_lib}/defaults"                         # defaults.rb
+    require "#{SiSU_lib}/particulars"                      # particulars.rb
     @@fns=nil
     def initialize(md,t_o)
       @md,@t_o=md,t_o
@@ -85,10 +86,13 @@ module SiSU_Format_Shared
     end
     def urls(data)
       @words=[]
+      map_nametags=SiSU_Particulars::Combined_singleton.instance.get_map_nametags(@md).nametags_map 
       data.each do |word|
-        @words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/
+        @words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
           if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
             m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/.match(word).captures
+          elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
+            m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/.match(word).captures
           elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}image/
             m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(image)/.match(word).captures
           end
@@ -107,12 +111,27 @@ module SiSU_Format_Shared
             end
             word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/,ins)
           else
+            u=case u
+            when /^http:\/\//
+              u
+            when /^:/
+              u.gsub!(/^:/,'')
+              "#{@env.url.root}/#{u}"
+            when /^\.\.\// # can remove
+              u.gsub!(/^\.\.\//,'')
+              "#{@env.url.root}/#{u}"
+            else
+              "#{@env.url.root}/#{@md.fnb}/#{map_nametags[u][:segname]}#{Sfx[:html]}##{u}"
+            end
             link=m[/(.+)/m]
             png=m.scan(/\S+/)[0].strip
             link=link.strip
             ins=%{<a href="#{u}">#{link}</a>}
             word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,ins)
+            word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,ins)
+            word
           end
+          word
         else word
         end
         word
@@ -134,8 +153,8 @@ module SiSU_Format_Shared
       s=t_o.obj
       if t_o.is !='code'
         s=markup_generic(s)
-        if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/
-          wm=s.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)|\S+/)
+        if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
+          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+/)
           words=urls(wm)
           s.gsub!(/.+/m,words)
         end
@@ -152,8 +171,8 @@ module SiSU_Format_Shared
     end
     def markup_note(s)
       s=markup_generic(s)
-      if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/
-        wm=s.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)|\S+/)
+      if s =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
+        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+/)
         words=urls(wm)
         s=s.gsub(/.+/m,words)
       end
diff --git a/lib/sisu/v2/shared_xml.rb b/lib/sisu/v2/shared_xml.rb
index 8a58909b..4d504882 100644
--- a/lib/sisu/v2/shared_xml.rb
+++ b/lib/sisu/v2/shared_xml.rb
@@ -372,6 +372,8 @@ module SiSU_XML_munge
         dob.obj.gsub!(/href="#{Xx[:segment]}/m,'href="')
         dob.obj.gsub!(/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{Mx[:rel_c]}]+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\.\.\/\S+?)#{Mx[:rel_c]}/,
           '<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="\2">\1</link>')
+        dob.obj.gsub!(/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{Mx[:rel_c]}]+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}:(\S+?)#{Mx[:rel_c]}/,
+          '<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="../\2">\1</link>')
         dob.obj.gsub!(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/,
           '<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="#\2">\1</link>')
         dob.obj.gsub!(/(?:^|[^_\\])#{Mx[:lnk_o]}[ ]*(\S+?\.(?:jpg|png|gif))[ ]+(\d+)x(\d+)(\s+[^}]+)?#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,
diff --git a/lib/sisu/v2/texpdf_format.rb b/lib/sisu/v2/texpdf_format.rb
index 1d2e891f..68190407 100644
--- a/lib/sisu/v2/texpdf_format.rb
+++ b/lib/sisu/v2/texpdf_format.rb
@@ -357,47 +357,53 @@ module SiSU_TeX_Pdf
     end
     def url_str_internal(str,idx=nil)
       map_nametags=SiSU_Particulars::Combined_singleton.instance.get_map_nametags(@md).nametags_map #p map_nametags
-      url_generic_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/m
-      url_relative_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\.\.\/\S+?#{Mx[:rel_c]}/m
-      url_internal_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}#?\S+?#{Mx[:rel_c]}/m
-      while str =~/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\.\.\/\S+?)#{Mx[:rel_c]}/m
+      rgx_url_generic=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/m
+      rgx_url_relative=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}:\S+?#{Mx[:rel_c]}/m
+      rgx_url_internal=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}#?\S+?#{Mx[:rel_c]}/m
+      while str =~/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+)#{Mx[:lnk_c]}#{Mx[:rel_o]}:(\S+?)#{Mx[:rel_c]}/m
         link,url=$1,$2
         link,url=link.strip,url.strip
         link.gsub!(/&/,"#{Xx[:protect]}&")
-        url.gsub!(/^(\.\.\/)/,"#{@env.url.root}/")
-        str.sub!(/#{Mx[:lnk_o]}[^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+#{Mx[:lnk_c]}#{Mx[:rel_o]}\.\.\/\S+?#{Mx[:rel_c]}/m,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}")
+        url="#{@env.url.root}/" + url
+        str.sub!(/#{Mx[:lnk_o]}[^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+#{Mx[:lnk_c]}#{Mx[:rel_o]}:\S+?#{Mx[:rel_c]}/m,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}")
       end
       while str =~/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+)#{Mx[:lnk_c]}#{Mx[:rel_o]}#?(\S+?)#{Mx[:rel_c]}/m
         link,url=$1,$2
         link,url=link.strip,url.strip
         link.gsub!(/&/,"#{Xx[:protect]}&")
+        url.gsub!(/\\_/,'_')
+        ocn_lnk=if map_nametags[url] \
+        and map_nametags[url][:ocn]
+          map_nametags[url][:ocn]
+        else nil
+        end
         ocn_lnk=(url=~/^\d+$/ ?  url : (map_nametags[url][:ocn]))
-        if not ocn_lnk.empty?
+        if ocn_lnk and not ocn_lnk.empty?
           idx \
-          ? (str.sub!(url_internal_rgx,"\\hyperlink{#{ocn_lnk}}{#{link}}")) \
-          : (str.sub!(url_internal_rgx,"#{@brace_rel.tex_open}\\hyperlink{#{ocn_lnk}}{#{link}}#{@brace_rel.tex_close}"))
+          ? (str.sub!(rgx_url_internal,"\\hyperlink{#{ocn_lnk}}{#{link}}")) \
+          : (str.sub!(rgx_url_internal,"#{@brace_rel.tex_open}\\hyperlink{#{ocn_lnk}}{#{link}}#{@brace_rel.tex_close}"))
         else
           puts %{name tag: "#{url}" not found}
-          str.sub!(url_internal_rgx,"#{link}")
+          str.sub!(rgx_url_internal,"#{link}")
         end
         #[keep] code that follows uses nametags directly, currently nametags converted to their ocn, related code: |texpdf.rb|@|hypertargets|
         #idx \
-        #? (str.sub!(url_internal_rgx,"\\hyperlink{#{url}}{#{link}}")) \
-        #: (str.sub!(url_internal_rgx,"#{@brace_rel.tex_open}\\hyperlink{#{url}}{#{link}}#{@brace_rel.tex_close}"))
+        #? (str.sub!(rgx_url_internal,"\\hyperlink{#{url}}{#{link}}")) \
+        #: (str.sub!(rgx_url_internal,"#{@brace_rel.tex_open}\\hyperlink{#{url}}{#{link}}#{@brace_rel.tex_close}"))
       end
       str.gsub!(/#{Xx[:protect]}/,'')
       str
     end
     def url_str(str)
-      url_generic_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
-      while str =~url_generic_rgx
-        if str=~url_generic_rgx
+      rgx_url_generic=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
+      while str =~rgx_url_generic
+        if str=~rgx_url_generic
           regx_url=%r/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/m
           z,url=regx_url.match(str).captures if str =~regx_url
           url=url.strip
           link=z.strip
           link.gsub!(/&/,"#{Xx[:protect]}&")
-          str.sub!(url_generic_rgx,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}")
+          str.sub!(rgx_url_generic,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}")
           str.gsub!(/#{Xx[:protect]}/,'')
           str
         else str
@@ -407,9 +413,9 @@ module SiSU_TeX_Pdf
       str
     end
     def url_with_txt(dob)
-      url_generic_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
-      while dob.tmp =~url_generic_rgx
-        if dob.tmp=~url_generic_rgx
+      rgx_url_generic=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
+      while dob.tmp =~rgx_url_generic
+        if dob.tmp=~rgx_url_generic
           if dob.tmp =~/#{Mx[:lnk_o]}(?:.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
             regx_url=%r/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/m
             punctuate=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m.match(dob.tmp).captures.join
@@ -421,7 +427,7 @@ module SiSU_TeX_Pdf
           url=url.strip
           link=z.strip
           link.gsub!(/&/,"#{Xx[:protect]}&")
-          dob.tmp.sub!(url_generic_rgx,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}#{punctuate}")
+          dob.tmp.sub!(rgx_url_generic,"#{@brace_rel.tex_open}\\href{#{url}}{#{link}}#{@brace_rel.tex_close}#{punctuate}")
           dob.tmp.gsub!(/#{Xx[:protect]}/,'')
           dob
         else dob
@@ -442,18 +448,18 @@ module SiSU_TeX_Pdf
       }
       images_hash={ }
       generic_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image\b)/m
-      url_generic_rgx=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
+      rgx_url_generic=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
       url_bare_rgx=/#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
       url_image_rgx=/#{Mx[:lnk_o]}[a-zA-Z0-9_\\-]+\.(?:png|jpg|gif).+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
       image_rgx=/#{Mx[:lnk_o]}[a-zA-Z0-9_\\-]+\.(?:png|jpg|gif).+?#{Mx[:lnk_c]}image/m
       @md.papersize_array.each do |ps|
         images_hash[ps] = dob.tmp
         while images_hash[ps] =~generic_rgx
-          if dob.tmp =~url_generic_rgx \
+          if dob.tmp =~rgx_url_generic \
           and dob.tmp !~/\.(?:png|jpg|gif)|#{Mx[:lnk_c]}image\b/m
             dob=url_with_txt(dob)
           elsif images_hash[ps]=~generic_rgx
-            if dob.tmp=~url_generic_rgx
+            if dob.tmp=~rgx_url_generic
               if images_hash[ps] =~/#{Mx[:lnk_o]}(?:.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m
                 regx_url=%r/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/m
                 punctuate=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/m.match(images_hash[ps]).captures.join
@@ -940,6 +946,7 @@ WOK
           w.gsub!(/[#{Mx[:br_line]}#{Mx[:br_paragraph]}]/,' \newline ') #watch
           w.gsub!(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/,'<=tilde>') #126 usual
           w.gsub!(/\\?\||#{Mx[:gl_o]}#124#{Mx[:gl_c]}/,'<=pipe>') #unless is=='code' #unless w=~/<~\d+;(?:[ohmu]|[0-6]:)\d+;\w\d+>/ # | SiSU not really special sisu character but done, also LaTeX
+          w.gsub!(/\#/,'<=hash>') unless w =~/#{Mx[:rel_o]}/
           para_array << w
         end
         str=para_array.join(' ')
@@ -971,7 +978,6 @@ WOK
       str.gsub!(/#{Mx[:gl_o]}#124#{Mx[:gl_c]}/,'|')                                                # | SiSU not really special sisu character but done, also LaTeX
       str.gsub!(/#{Mx[:gl_o]}#058#{Mx[:gl_c]}/,':')                                                # : SiSU not really special sisu character but done, also LaTeX
       str.gsub!(/#{Mx[:gl_o]}#094#{Mx[:gl_c]}|\^/,'<=caret>')                                      # ^ SiSU not really special sisu character but done, also LaTeX
-      str.gsub!(/\#/,'<=hash>')
       ##watch placement, problem sequence ^
       str.gsub!(/<sup><font face=symbol>&atild;<\/font><\/sup>/,' ')
       str.gsub!(/\\copy(right|mark)?/,'<=copymark>') # ok problem with superscript
@@ -1101,7 +1107,7 @@ WOK
     def special_word_break_points
       str=@txt
       str.gsub!(/([_,.;:\/|=])/,'\1\-')
-      str.gsub!(/(--)(\S{4,)/,'\1\-\2')
+      str.gsub!(/(--)(\S{4,})/,'\1\-\2')
       @txt=str
     end
     def special_number_break_points
diff --git a/lib/sisu/v2/webrick.rb b/lib/sisu/v2/webrick.rb
index 05aac1ee..ff228a65 100644
--- a/lib/sisu/v2/webrick.rb
+++ b/lib/sisu/v2/webrick.rb
@@ -168,7 +168,9 @@ WOK
   puts "        #{@cX.off}"
   get=@argv.shift if @argv[0] =~/pwd/                      #% argv
   brick(@port,get)
-rescue; SiSU_Errors::Info_error.new($!,$@,'-W',nil).error #fix
+rescue;
+  require "#{SiSU_lib}/sysenv"                             # sysenv.rb
+  SiSU_Errors::Info_error.new($!,$@,'-W',nil).error #fix
 ensure
 end
 __END__
-- 
cgit v1.2.3