From 99c5b5d56f8785ee9ec2cc991c55a8a2eb6524bb Mon Sep 17 00:00:00 2001
From: Ralph Amissah <ralph@amissah.com>
Date: Wed, 5 Feb 2014 01:02:31 -0500
Subject: v5 v6: html, endnote name and nameref make compliant (epub use same)

---
 data/doc/sisu/CHANGELOG_v5          |  3 +++
 data/doc/sisu/CHANGELOG_v6          |  3 +++
 lib/sisu/v5/constants.rb            |  6 ++++++
 lib/sisu/v5/html_format.rb          |  2 +-
 lib/sisu/v5/html_segments.rb        |  6 +++---
 lib/sisu/v5/html_tune.rb            | 12 ++++++------
 lib/sisu/v5/xhtml_epub2_format.rb   |  6 +++---
 lib/sisu/v5/xhtml_epub2_segments.rb |  2 +-
 lib/sisu/v5/xhtml_epub2_tune.rb     | 20 ++++++++++----------
 lib/sisu/v6/constants.rb            |  6 ++++++
 lib/sisu/v6/html_format.rb          |  2 +-
 lib/sisu/v6/html_segments.rb        |  6 +++---
 lib/sisu/v6/html_tune.rb            | 12 ++++++------
 lib/sisu/v6/xhtml_epub2_format.rb   |  6 +++---
 lib/sisu/v6/xhtml_epub2_segments.rb |  2 +-
 lib/sisu/v6/xhtml_epub2_tune.rb     | 20 ++++++++++----------
 16 files changed, 66 insertions(+), 48 deletions(-)

diff --git a/data/doc/sisu/CHANGELOG_v5 b/data/doc/sisu/CHANGELOG_v5
index e59d839d..a97c8bc1 100644
--- a/data/doc/sisu/CHANGELOG_v5
+++ b/data/doc/sisu/CHANGELOG_v5
@@ -48,6 +48,9 @@ http://www.jus.uio.no/sisu/pkg/src/sisu_5.3.1.orig.tar.xz
   * --strict html option
     (various changes, e.g. ocn [url]#37 only available as [url]#o37)
   * tags
+  * endnote name and nameref (make compliant)
+
+* epub use same endnote name and nameref as introduced here for html
 
 * ocn turn on/off rules
   * txt
diff --git a/data/doc/sisu/CHANGELOG_v6 b/data/doc/sisu/CHANGELOG_v6
index a1183d8f..3000f42a 100644
--- a/data/doc/sisu/CHANGELOG_v6
+++ b/data/doc/sisu/CHANGELOG_v6
@@ -38,6 +38,9 @@ http://www.jus.uio.no/sisu/pkg/src/sisu_6.0.1.orig.tar.xz
   * --strict html option
     (various changes, e.g. ocn [url]#37 only available as [url]#o37)
   * tags
+  * endnote name and nameref (make compliant)
+
+* epub use same endnote name and nameref as introduced here for html
 
 * ocn turn on/off rules
   * txt
diff --git a/lib/sisu/v5/constants.rb b/lib/sisu/v5/constants.rb
index fea57ce8..a9be54f0 100644
--- a/lib/sisu/v5/constants.rb
+++ b/lib/sisu/v5/constants.rb
@@ -102,6 +102,12 @@ Xx={
 }
 Mx={
   ocn_id_char:               'o',
+  note:                      'note_',
+  note_ref:                  'noteref_',
+  note_astx:                 'note_astx_',
+  note_ref_astx:             'noteref_astx_',
+  note_plus:                 'note_plus_',
+  note_ref_plus:             'noteref_plus_',
   meta_o:                    '〔@',   meta_c: '〕',
   lv_o_0:                    0,
   lv_o_1:                    1,
diff --git a/lib/sisu/v5/html_format.rb b/lib/sisu/v5/html_format.rb
index e0be4f8a..a933d091 100644
--- a/lib/sisu/v5/html_format.rb
+++ b/lib/sisu/v5/html_format.rb
@@ -1248,7 +1248,7 @@ WOK
       note=''
       if txt =~/(#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})/m # had \s* at end
         note=$1
-        note=note.gsub(/[\n\s]+/m,' ')
+        note=note.gsub(/[\s]+/m,' ')
         txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ').
           gsub(/<a[\n\s]+name="-\d+"[\n\s]+href="#_\d+">&nbsp;<sup>\d+<\/sup>&nbsp;/m,'')
       end
diff --git a/lib/sisu/v5/html_segments.rb b/lib/sisu/v5/html_segments.rb
index b710db81..7a368b9d 100644
--- a/lib/sisu/v5/html_segments.rb
+++ b/lib/sisu/v5/html_segments.rb
@@ -502,7 +502,7 @@ module SiSU_HTML_Seg
           '<br /><hr width=90% /><br />'
         end
         if @md.flag_separate_endnotes
-          dob.obj=dob.obj.gsub(/"\s+href="#_(\d+)">/,%{" href=\"endnotes#{Sfx[:html]}#_\\1">})       #endnote- twice #removed file type
+          dob.obj=dob.obj.gsub(/"\s+href="#(#{Mx[:note]}\d+)">/,%{" href=\"endnotes#{Sfx[:html]}#\\1">})       #endnote- twice #removed file type
         end
         if dob.obj !~/#{@vz.margin_txt_w1}|#{@vz.margin_txt_w2}/
           if (dob.is==:heading \
@@ -600,7 +600,7 @@ module SiSU_HTML_Seg
           @@seg_subtoc_array << subtoc
         end
         if @md.flag_auto_endnotes
-          if (dob.obj =~/(?:#{Mx[:en_a_o]}|#{Mx[:en_b_o]})[\d*+]+ <a name="_[\d*+]+"/) \
+          if (dob.obj =~/(?:#{Mx[:en_a_o]}|#{Mx[:en_b_o]})[\d*+]+ <a name="#{Mx[:note]}[\d*+]+"/) \
           && dob.is !=:code # endnote-
             endnote_array=[]
             if dob.obj=~/#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}/m
@@ -629,7 +629,7 @@ module SiSU_HTML_Seg
               end
               try.join('<br \/>')
               #% creation of separate end segment/page of all endnotes referenced back to reference segment
-              m=/(?:#{Mx[:en_a_o]}[\d*+]+|#{Mx[:en_b_o]}[*+]\d+)\s+(.+?href=")(#-[\d*+]+".+)(?:#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/mi
+              m=/(?:#{Mx[:en_a_o]}[\d*+]+|#{Mx[:en_b_o]}[*+]\d+)\s+(.+?href=")(##{Mx[:note_ref]}[\d*+]+".+)(?:#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/mi
               endnote_part_a=note_match_seg[m,1]
               endnote_part_b=note_match_seg[m,2]
               txt_obj={ endnote_part_a: endnote_part_a, endnote_part_b: endnote_part_b }
diff --git a/lib/sisu/v5/html_tune.rb b/lib/sisu/v5/html_tune.rb
index e9392ca8..84336861 100644
--- a/lib/sisu/v5/html_tune.rb
+++ b/lib/sisu/v5/html_tune.rb
@@ -256,14 +256,14 @@ module SiSU_HTML_Tune
     def endnotes_html(dob)
       unless dob.is ==:code
         dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?)(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2.</sup></a> \\3 \\4}).
           gsub(/(#{Mx[:en_b_o]})([*+]\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2.</sup></a> \\3 \\4}).
           gsub(/(#{Mx[:en_a_o]})([*+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2</sup></a> \3 \4')    #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2</sup></a> \\3 \\4})
       end
       dob
     end
diff --git a/lib/sisu/v5/xhtml_epub2_format.rb b/lib/sisu/v5/xhtml_epub2_format.rb
index 8dd8a88e..e3b877aa 100644
--- a/lib/sisu/v5/xhtml_epub2_format.rb
+++ b/lib/sisu/v5/xhtml_epub2_format.rb
@@ -2132,10 +2132,10 @@ output_epub_cont_seg.close
       note=''
       if txt =~/(#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})/m # had \s* at end
         note=$1
-        note=note.gsub(/[\n\s]+/m,' ')
+        note=note.gsub(/[\s]+/m,' ')
         txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ').
-          gsub(/<a[\n\s]+"[\n\s]+href="#note_ref\d+">#{$ep[:hsp]}<sup id="note\d+">\d+<\/sup>#{$ep[:hsp]}/m,'').
-          gsub(/<a[\n\s]+"[\n\s]+href="#note_ref\d+">#{$ep[:hsp]}<sup id="note\d+">\d+<\/sup>#{$ep[:hsp]}/m,'') #remove
+          gsub(/<a[\n\s]+"[\n\s]+href="##{Mx[:note_ref]}\d+">#{$ep[:hsp]}<sup id="#{Mx[:note]}\d+">\d+<\/sup>#{$ep[:hsp]}/m,'').
+          gsub(/<a[\n\s]+"[\n\s]+href="##{Mx[:note_ref]}\d+">#{$ep[:hsp]}<sup id="#{Mx[:note]}\d+">\d+<\/sup>#{$ep[:hsp]}/m,'') #remove
       end
       %{<#{tag} class="#{attrib}">
     <a href="#o#{@ocn}"><i>#{txt}</i></a> #{note}
diff --git a/lib/sisu/v5/xhtml_epub2_segments.rb b/lib/sisu/v5/xhtml_epub2_segments.rb
index 764fcc17..8612696f 100644
--- a/lib/sisu/v5/xhtml_epub2_segments.rb
+++ b/lib/sisu/v5/xhtml_epub2_segments.rb
@@ -401,7 +401,7 @@ WOK
           sto.break
       end
       if @md.flag_separate_endnotes # may need to revisit, check
-        dob.obj=dob.obj.gsub(/"\s+href="#note_ref(\d+)">/,%{" href=\"endnotes#{Sfx[:epub_xhtml]}#note_ref\\1">})       #endnote- twice #removed file type
+        dob.obj=dob.obj.gsub(/"\s+href="##{Mx[:note_ref]}(\d+)">/,%{" href=\"endnotes#{Sfx[:epub_xhtml]}##{Mx[:note_ref]}\\1">})       #endnote- twice #removed file type
       end
       if (dob.is ==:heading \
       || dob.is==:heading_insert \
diff --git a/lib/sisu/v5/xhtml_epub2_tune.rb b/lib/sisu/v5/xhtml_epub2_tune.rb
index 6ac53c39..82f90366 100644
--- a/lib/sisu/v5/xhtml_epub2_tune.rb
+++ b/lib/sisu/v5/xhtml_epub2_tune.rb
@@ -298,22 +298,22 @@ module SiSU_XHTML_EPUB2_Tune
       data.each do |dob|
         unless dob.is ==:code
           dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?)(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note\\2">#{Mx[:nbsp]}<sup id="note_ref\\2">\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1\\2 <a href="#note_ref\\2">#{Mx[:nbsp]}<sup id="note\\2">\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}\\2">\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1\\2 <a href="##{Mx[:note_ref]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note]}\\2">\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
             gsub(/(#{Mx[:en_b_o]})[*](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note_astx\\2">#{Mx[:nbsp]}<sup id="note_ref_astx\\2">#{ast}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1#{ast}\\2 <a href="#note_ref_astx\\2">#{Mx[:nbsp]}<sup id="note_astx\\2">#{ast}\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_astx]}\\2">#{ast}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1#{ast}\\2 <a href="##{Mx[:note_ref_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_astx]}\\2">#{ast}\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
             gsub(/(#{Mx[:en_b_o]})[+](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note_plus\\2">#{Mx[:nbsp]}<sup id="note_ref_plus\\2">#{pls}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1#{pls}\\2 <a href="#note_ref_plus\\2">#{Mx[:nbsp]}<sup id="note_plus\\2">#{pls}\\2.</sup></a> \\3 \\4}) #endnote- note- (careful may have switched) # double-check there may here be a bug
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_plus]}\\2">#{pls}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1#{pls}\\2 <a href="##{Mx[:note_ref_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_plus]}\\2">#{pls}\\2.</sup></a> \\3 \\4}) #endnote- note- (careful may have switched) # double-check there may here be a bug
           if dob.obj =~/#{Mx[:en_a_o]}([*+]+)\s+.+?#{Mx[:en_a_c]}/
             m=$1.length.to_i
             dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]})[*]+\s+(.+?)(#{Mx[:en_a_c]})/,
-                %{#{Mx[:nbsp]}<a href="#note#{a*m}">#{Mx[:nbsp]}<sup id="note_ref#{a*m}">#{ast*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-                %{\\1#{ast*m} <a href="#note_ref#{a*m}">#{Mx[:nbsp]}<sup id="note#{a*m}">#{ast*m}</sup></a> \\2 \\3}). #endnote- note- (careful may have switched)
+                %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{a*m}">#{ast*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+                %{\\1#{ast*m} <a href="##{Mx[:note_ref]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{a*m}">#{ast*m}</sup></a> \\2 \\3}). #endnote- note- (careful may have switched)
               gsub(/(#{Mx[:en_a_o]})([+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
-                %{#{Mx[:nbsp]}<a href="#note#{s*m}">#{Mx[:nbsp]}<sup id="note_ref#{s*m}">#{pls*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-                %{\\1#{pls*m} <a href="#note_ref#{s*m}">#{Mx[:nbsp]}<sup id="note#{s*m}">#{pls*m}</sup></a> \\2 \\3}) #endnote- note- (careful may have switched)
+                %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{s*m}">#{pls*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+                %{\\1#{pls*m} <a href="##{Mx[:note_ref]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{s*m}">#{pls*m}</sup></a> \\2 \\3}) #endnote- note- (careful may have switched)
           end
         end
         @tuned_file << dob
diff --git a/lib/sisu/v6/constants.rb b/lib/sisu/v6/constants.rb
index 390df5a4..e70a2f98 100644
--- a/lib/sisu/v6/constants.rb
+++ b/lib/sisu/v6/constants.rb
@@ -102,6 +102,12 @@ Xx={
 }
 Mx={
   ocn_id_char:               'o',
+  note:                      'note_',
+  note_ref:                  'noteref_',
+  note_astx:                 'note_astx_',
+  note_ref_astx:             'noteref_astx_',
+  note_plus:                 'note_plus_',
+  note_ref_plus:             'noteref_plus_',
   meta_o:                    '〔@',   meta_c: '〕',
   lv_o_0:                    0,
   lv_o_1:                    1,
diff --git a/lib/sisu/v6/html_format.rb b/lib/sisu/v6/html_format.rb
index ddfaaa70..63271d95 100644
--- a/lib/sisu/v6/html_format.rb
+++ b/lib/sisu/v6/html_format.rb
@@ -1248,7 +1248,7 @@ WOK
       note=''
       if txt =~/(#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})/m # had \s* at end
         note=$1
-        note=note.gsub(/[\n\s]+/m,' ')
+        note=note.gsub(/[\s]+/m,' ')
         txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ').
           gsub(/<a[\n\s]+name="-\d+"[\n\s]+href="#_\d+">&nbsp;<sup>\d+<\/sup>&nbsp;/m,'')
       end
diff --git a/lib/sisu/v6/html_segments.rb b/lib/sisu/v6/html_segments.rb
index efe37024..0586cdf6 100644
--- a/lib/sisu/v6/html_segments.rb
+++ b/lib/sisu/v6/html_segments.rb
@@ -502,7 +502,7 @@ module SiSU_HTML_Seg
           '<br /><hr width=90% /><br />'
         end
         if @md.flag_separate_endnotes
-          dob.obj=dob.obj.gsub(/"\s+href="#_(\d+)">/,%{" href=\"endnotes#{Sfx[:html]}#_\\1">})       #endnote- twice #removed file type
+          dob.obj=dob.obj.gsub(/"\s+href="#(#{Mx[:note]}\d+)">/,%{" href=\"endnotes#{Sfx[:html]}#\\1">})       #endnote- twice #removed file type
         end
         if dob.obj !~/#{@vz.margin_txt_w1}|#{@vz.margin_txt_w2}/
           if (dob.is==:heading \
@@ -600,7 +600,7 @@ module SiSU_HTML_Seg
           @@seg_subtoc_array << subtoc
         end
         if @md.flag_auto_endnotes
-          if (dob.obj =~/(?:#{Mx[:en_a_o]}|#{Mx[:en_b_o]})[\d*+]+ <a name="_[\d*+]+"/) \
+          if (dob.obj =~/(?:#{Mx[:en_a_o]}|#{Mx[:en_b_o]})[\d*+]+ <a name="#{Mx[:note]}[\d*+]+"/) \
           && dob.is !=:code # endnote-
             endnote_array=[]
             if dob.obj=~/#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}/m
@@ -629,7 +629,7 @@ module SiSU_HTML_Seg
               end
               try.join('<br \/>')
               #% creation of separate end segment/page of all endnotes referenced back to reference segment
-              m=/(?:#{Mx[:en_a_o]}[\d*+]+|#{Mx[:en_b_o]}[*+]\d+)\s+(.+?href=")(#-[\d*+]+".+)(?:#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/mi
+              m=/(?:#{Mx[:en_a_o]}[\d*+]+|#{Mx[:en_b_o]}[*+]\d+)\s+(.+?href=")(##{Mx[:note_ref]}[\d*+]+".+)(?:#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/mi
               endnote_part_a=note_match_seg[m,1]
               endnote_part_b=note_match_seg[m,2]
               txt_obj={ endnote_part_a: endnote_part_a, endnote_part_b: endnote_part_b }
diff --git a/lib/sisu/v6/html_tune.rb b/lib/sisu/v6/html_tune.rb
index 384a19b0..516deaa5 100644
--- a/lib/sisu/v6/html_tune.rb
+++ b/lib/sisu/v6/html_tune.rb
@@ -256,14 +256,14 @@ module SiSU_HTML_Tune
     def endnotes_html(dob)
       unless dob.is ==:code
         dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?)(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2.</sup></a> \\3 \\4}).
           gsub(/(#{Mx[:en_b_o]})([*+]\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2.</sup></a> \\3 \\4}).
           gsub(/(#{Mx[:en_a_o]})([*+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
-            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
-            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2</sup></a> \3 \4')    #endnote- note- (careful may have switched)
+            %{&nbsp;<a name="#{Mx[:note_ref]}\\2" href="##{Mx[:note]}\\2">&nbsp;<sup>\\2</sup>&nbsp;</a> } +
+            %{\\1\\2 <a name="#{Mx[:note]}\\2" href="##{Mx[:note_ref]}\\2">&nbsp;<sup>\\2</sup></a> \\3 \\4})
       end
       dob
     end
diff --git a/lib/sisu/v6/xhtml_epub2_format.rb b/lib/sisu/v6/xhtml_epub2_format.rb
index d5b7e85e..7ae97f95 100644
--- a/lib/sisu/v6/xhtml_epub2_format.rb
+++ b/lib/sisu/v6/xhtml_epub2_format.rb
@@ -2132,10 +2132,10 @@ output_epub_cont_seg.close
       note=''
       if txt =~/(#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})/m # had \s* at end
         note=$1
-        note=note.gsub(/[\n\s]+/m,' ')
+        note=note.gsub(/[\s]+/m,' ')
         txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ').
-          gsub(/<a[\n\s]+"[\n\s]+href="#note_ref\d+">#{$ep[:hsp]}<sup id="note\d+">\d+<\/sup>#{$ep[:hsp]}/m,'').
-          gsub(/<a[\n\s]+"[\n\s]+href="#note_ref\d+">#{$ep[:hsp]}<sup id="note\d+">\d+<\/sup>#{$ep[:hsp]}/m,'') #remove
+          gsub(/<a[\n\s]+"[\n\s]+href="##{Mx[:note_ref]}\d+">#{$ep[:hsp]}<sup id="#{Mx[:note]}\d+">\d+<\/sup>#{$ep[:hsp]}/m,'').
+          gsub(/<a[\n\s]+"[\n\s]+href="##{Mx[:note_ref]}\d+">#{$ep[:hsp]}<sup id="#{Mx[:note]}\d+">\d+<\/sup>#{$ep[:hsp]}/m,'') #remove
       end
       %{<#{tag} class="#{attrib}">
     <a href="#o#{@ocn}"><i>#{txt}</i></a> #{note}
diff --git a/lib/sisu/v6/xhtml_epub2_segments.rb b/lib/sisu/v6/xhtml_epub2_segments.rb
index 2be4afd9..d249b0ff 100644
--- a/lib/sisu/v6/xhtml_epub2_segments.rb
+++ b/lib/sisu/v6/xhtml_epub2_segments.rb
@@ -401,7 +401,7 @@ WOK
           sto.break
       end
       if @md.flag_separate_endnotes # may need to revisit, check
-        dob.obj=dob.obj.gsub(/"\s+href="#note_ref(\d+)">/,%{" href=\"endnotes#{Sfx[:epub_xhtml]}#note_ref\\1">})       #endnote- twice #removed file type
+        dob.obj=dob.obj.gsub(/"\s+href="##{Mx[:note_ref]}(\d+)">/,%{" href=\"endnotes#{Sfx[:epub_xhtml]}##{Mx[:note_ref]}\\1">})       #endnote- twice #removed file type
       end
       if (dob.is ==:heading \
       || dob.is==:heading_insert \
diff --git a/lib/sisu/v6/xhtml_epub2_tune.rb b/lib/sisu/v6/xhtml_epub2_tune.rb
index 12f70fcf..fd90ae54 100644
--- a/lib/sisu/v6/xhtml_epub2_tune.rb
+++ b/lib/sisu/v6/xhtml_epub2_tune.rb
@@ -298,22 +298,22 @@ module SiSU_XHTML_EPUB2_Tune
       data.each do |dob|
         unless dob.is ==:code
           dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?)(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note\\2">#{Mx[:nbsp]}<sup id="note_ref\\2">\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1\\2 <a href="#note_ref\\2">#{Mx[:nbsp]}<sup id="note\\2">\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}\\2">\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1\\2 <a href="##{Mx[:note_ref]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note]}\\2">\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
             gsub(/(#{Mx[:en_b_o]})[*](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note_astx\\2">#{Mx[:nbsp]}<sup id="note_ref_astx\\2">#{ast}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1#{ast}\\2 <a href="#note_ref_astx\\2">#{Mx[:nbsp]}<sup id="note_astx\\2">#{ast}\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_astx]}\\2">#{ast}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1#{ast}\\2 <a href="##{Mx[:note_ref_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_astx]}\\2">#{ast}\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
             gsub(/(#{Mx[:en_b_o]})[+](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
-              %{#{Mx[:nbsp]}<a href="#note_plus\\2">#{Mx[:nbsp]}<sup id="note_ref_plus\\2">#{pls}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-              %{\\1#{pls}\\2 <a href="#note_ref_plus\\2">#{Mx[:nbsp]}<sup id="note_plus\\2">#{pls}\\2.</sup></a> \\3 \\4}) #endnote- note- (careful may have switched) # double-check there may here be a bug
+              %{#{Mx[:nbsp]}<a href="##{Mx[:note_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_plus]}\\2">#{pls}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+              %{\\1#{pls}\\2 <a href="##{Mx[:note_ref_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_plus]}\\2">#{pls}\\2.</sup></a> \\3 \\4}) #endnote- note- (careful may have switched) # double-check there may here be a bug
           if dob.obj =~/#{Mx[:en_a_o]}([*+]+)\s+.+?#{Mx[:en_a_c]}/
             m=$1.length.to_i
             dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]})[*]+\s+(.+?)(#{Mx[:en_a_c]})/,
-                %{#{Mx[:nbsp]}<a href="#note#{a*m}">#{Mx[:nbsp]}<sup id="note_ref#{a*m}">#{ast*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-                %{\\1#{ast*m} <a href="#note_ref#{a*m}">#{Mx[:nbsp]}<sup id="note#{a*m}">#{ast*m}</sup></a> \\2 \\3}). #endnote- note- (careful may have switched)
+                %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{a*m}">#{ast*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+                %{\\1#{ast*m} <a href="##{Mx[:note_ref]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{a*m}">#{ast*m}</sup></a> \\2 \\3}). #endnote- note- (careful may have switched)
               gsub(/(#{Mx[:en_a_o]})([+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
-                %{#{Mx[:nbsp]}<a href="#note#{s*m}">#{Mx[:nbsp]}<sup id="note_ref#{s*m}">#{pls*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
-                %{\\1#{pls*m} <a href="#note_ref#{s*m}">#{Mx[:nbsp]}<sup id="note#{s*m}">#{pls*m}</sup></a> \\2 \\3}) #endnote- note- (careful may have switched)
+                %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{s*m}">#{pls*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
+                %{\\1#{pls*m} <a href="##{Mx[:note_ref]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{s*m}">#{pls*m}</sup></a> \\2 \\3}) #endnote- note- (careful may have switched)
           end
         end
         @tuned_file << dob
-- 
cgit v1.2.3