aboutsummaryrefslogtreecommitdiffhomepage
path: root/org/object_munge.org
blob: 29fcdd732eb07300927c8ed2c12879d4d7444039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
-*- mode: org -*-
#+TITLE:       sisu object munge
#+DESCRIPTION: documents - structuring, various output representations & search
#+FILETAGS:    :sisu:munge:objects:
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT:   Copyright (C) 2015 - 2021 Ralph Amissah
#+LANGUAGE:    en
#+STARTUP:     content hideblocks hidestars noindent entitiespretty
#+OPTIONS:     H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+PROPERTY:    header-args  :exports code
#+PROPERTY:    header-args+ :noweb yes
#+PROPERTY:    header-args+ :eval no
#+PROPERTY:    header-args+ :results no
#+PROPERTY:    header-args+ :cache no
#+PROPERTY:    header-args+ :padline no
#+PROPERTY:    header-args+ :mkdirp yes

* object_munge.rb

#+HEADER: :tangle "../lib/sisu/object_munge.rb"
#+BEGIN_SRC ruby
#<<sisu_document_header>>
module SiSU_Object_Munge
  def i_src_o_strip_markup(txtobj)
    txtobj=txtobj.
      gsub(/#{Mx[:srcrgx_bold_o]}(.+?)#{Mx[:srcrgx_bold_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_italics_o]}(.+?)#{Mx[:srcrgx_italics_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_underscore_o]}(.+?)#{Mx[:srcrgx_underscore_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_cite_o]}(.+?)#{Mx[:srcrgx_cite_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_insert_o]}(.+?)#{Mx[:srcrgx_insert_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_strike_o]}(.+?)#{Mx[:srcrgx_strike_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_superscript_o]}(\d+)#{Mx[:srcrgx_superscript_c]}/m,'[\1]').
      gsub(/#{Mx[:srcrgx_superscript_o]}(.+?)#{Mx[:srcrgx_superscript_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_subscript_o]}(.+?)#{Mx[:srcrgx_subscript_c]}/m,'\1').
      gsub(/#{Mx[:srcrgx_hilite_o]}(.+?)#{Mx[:srcrgx_hilite_c]}/m,'\1').
      gsub(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/i,'~').
      gsub(/#{Mx[:en_a_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_a_c]}/m,''). # endnote removed
      gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/m,''). # endnote removed
      gsub(/(?:#{Mx[:nbsp]})+/,' ').
      gsub(/(?:#{Mx[:br_nl]})+/,"\n").
      gsub(/(?:#{Mx[:br_paragraph]})+/,"\n").
      gsub(/(?:#{Mx[:br_line]})+/,"\n").
      gsub(/#{Mx[:gl_o]}(?:#lt|#060)#{Mx[:gl_c]}/,'<').
      gsub(/#{Mx[:gl_o]}(?:#gt|#062)#{Mx[:gl_c]}/,'>').
      gsub(/#{Mx[:gl_o]}#(?:038|amp)#{Mx[:gl_c]}/,'&').
      gsub(/#{Mx[:gl_o]}#033#{Mx[:gl_c]}/,'!').
      gsub(/#{Mx[:gl_o]}#035#{Mx[:gl_c]}/,'#').
      gsub(/#{Mx[:gl_o]}#042#{Mx[:gl_c]}/,'*').
      gsub(/#{Mx[:gl_o]}#045#{Mx[:gl_c]}/,'-').
      gsub(/#{Mx[:gl_o]}#047#{Mx[:gl_c]}/,'/').
      gsub(/#{Mx[:gl_o]}#095#{Mx[:gl_c]}/,'_').
      gsub(/#{Mx[:gl_o]}#123#{Mx[:gl_c]}/,'{').
      gsub(/#{Mx[:gl_o]}#125#{Mx[:gl_c]}/,'}').
      gsub(/#{Mx[:gl_o]}#126#{Mx[:gl_c]}/,'~').
      gsub(/#{Mx[:gl_o]}#169#{Mx[:gl_c]}/,'©').
      gsub(/[ ][ ]s+/,' ').
      strip
if txtobj =~/Reading this/
  puts txtobj
  if txtobj =~ /#{Mx[:srcrgx_italics_o]}(.+?)#{Mx[:srcrgx_italics_c]}/
    puts __LINE__
    puts Mx[:srcrgx_italics_o]
    puts txtobj
  end
end
; txtobj
  end
  def i_ao_o_strip_markup(txtobj)
    txtobj=txtobj.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'\1').
      gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'\1').
      gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1').
      gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1').
      gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1').
      gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1').
      gsub(/#{Mx[:fa_superscript_o]}(\d+)#{Mx[:fa_superscript_c]}/,'[\1]').
      gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'\1').
      gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'\1').
      gsub(/#{Mx[:fa_hilite_o]}(.+?)#{Mx[:fa_hilite_c]}/,'\1').
      gsub(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/i,'~').
      gsub(/#{Mx[:en_a_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_a_c]}/,''). # endnote removed
      gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/,''). # endnote removed
      gsub(/(?:#{Mx[:nbsp]})+/,' ').
      gsub(/(?:#{Mx[:br_nl]})+/,"\n").
      gsub(/(?:#{Mx[:br_paragraph]})+/,"\n").
      gsub(/(?:#{Mx[:br_line]})+/,"\n").
      gsub(/#{Mx[:gl_o]}(?:#lt|#060)#{Mx[:gl_c]}/,'<').
      gsub(/#{Mx[:gl_o]}(?:#gt|#062)#{Mx[:gl_c]}/,'>').
      gsub(/#{Mx[:gl_o]}#(?:038|amp)#{Mx[:gl_c]}/,'&').
      gsub(/#{Mx[:gl_o]}#033#{Mx[:gl_c]}/,'!').
      gsub(/#{Mx[:gl_o]}#035#{Mx[:gl_c]}/,'#').
      gsub(/#{Mx[:gl_o]}#042#{Mx[:gl_c]}/,'*').
      gsub(/#{Mx[:gl_o]}#045#{Mx[:gl_c]}/,'-').
      gsub(/#{Mx[:gl_o]}#047#{Mx[:gl_c]}/,'/').
      gsub(/#{Mx[:gl_o]}#095#{Mx[:gl_c]}/,'_').
      gsub(/#{Mx[:gl_o]}#123#{Mx[:gl_c]}/,'{').
      gsub(/#{Mx[:gl_o]}#125#{Mx[:gl_c]}/,'}').
      gsub(/#{Mx[:gl_o]}#126#{Mx[:gl_c]}/,'~').
      gsub(/#{Mx[:gl_o]}#169#{Mx[:gl_c]}/,'©').
      gsub(/[ ][ ]s+/,' ').
      strip
  end
  def i_ao_o_src_markup_restore(txtobj)
    @txtobj=txtobj
    def textface_marks
      @txtobj.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'*{\1}*').
        gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'/{\1}/').
        gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'_{\1}_').
        gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'"{\1}"').
        gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'+{\1}+').
        gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'-{\1}-').
        gsub(/#{Mx[:fa_superscript_o]}(\d+)#{Mx[:fa_superscript_c]}/,'^{[\1]}^').
        gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'^{\1}^').
        gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,',{\1},').
        gsub(/#{Mx[:fa_hilite_o]}(.+?)#{Mx[:fa_hilite_c]}/,'\1').
        gsub(/#{Mx[:gl_o]}#(?:126|152)#{Mx[:gl_c]}/i,'~').
        gsub(/#{Mx[:en_a_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_a_c]}/,'~{\1 \2}~').
        gsub(/#{Mx[:en_b_o]}([\d*+]+)\s+(?:.+?)#{Mx[:en_b_c]}/,''). # endnote removed
        gsub(/(?:#{Mx[:nbsp]})+/,' ').
        gsub(/(?:#{Mx[:br_nl]})+/,"\n").
        gsub(/(?:#{Mx[:br_paragraph]})+/,"\n").
        gsub(/(?:#{Mx[:br_line]})+/,"\n").
        gsub(/#{Mx[:gl_o]}(?:#lt|#060)#{Mx[:gl_c]}/,'<').
        gsub(/#{Mx[:gl_o]}(?:#gt|#062)#{Mx[:gl_c]}/,'>').
        gsub(/#{Mx[:gl_o]}#(?:038|amp)#{Mx[:gl_c]}/,'&').
        gsub(/#{Mx[:gl_o]}#033#{Mx[:gl_c]}/,'!').
        gsub(/#{Mx[:gl_o]}#035#{Mx[:gl_c]}/,'#').
        gsub(/#{Mx[:gl_o]}#042#{Mx[:gl_c]}/,'*').
        gsub(/#{Mx[:gl_o]}#045#{Mx[:gl_c]}/,'-').
        gsub(/#{Mx[:gl_o]}#047#{Mx[:gl_c]}/,'/').
        gsub(/#{Mx[:gl_o]}#095#{Mx[:gl_c]}/,'_').
        gsub(/#{Mx[:gl_o]}#123#{Mx[:gl_c]}/,'{').
        gsub(/#{Mx[:gl_o]}#125#{Mx[:gl_c]}/,'}').
        gsub(/#{Mx[:gl_o]}#126#{Mx[:gl_c]}/,'~').
        gsub(/#{Mx[:gl_o]}#169#{Mx[:gl_c]}/,'©').
        gsub(/[ ][ ]s+/,' ').
        strip
    end
    def object_marks
      @txtobj
    end
    self
  end
  def clean_text(txtobj,markup=:ao)
    if txtobj.class==String
      txtobj=if markup ==:ao
        i_ao_o_strip_markup(txtobj)
      elsif markup ==:src
        i_src_o_strip_markup(txtobj)
      else p __FILE__; p __LINE__
      end
    elsif txtobj.class.inspect=~/^SiSU_AO_DocumentStructure::/
      txtobj.obj=i_ao_o_strip_markup(txtobj.obj)
    else p 'error'
    end
    txtobj
  end
  def footnotes_inline(txtobj)
  end
  def footnotes_ref_and_note(txtobj)
  end
  def src_markup(txtobj)
    txtobj
  end
  def extract_endnotes(doc_obj_txt,endnotes_)               #% used for extraction of endnotes from paragraphs
    if endnotes_ ==:separate
      notes_a=doc_obj_txt.scan(/#{Mx[:en_a_o]}([\d]+\s+.+?)#{Mx[:en_a_c]}/)
      ##notes_a=doc_obj_txt.scan(/#{Mx[:en_a_o]}([\d*+]+\s+.+?)#{Mx[:en_a_c]}/)
      #notes_b=doc_obj_txt.scan(/#{Mx[:en_b_o]}([\d*+]+\s+.+?)#{Mx[:en_b_c]}/)
      n=[]
      notes_a.flatten.each do |note| #high cost to deal with <br> appropriately within plaintext, consider
        note=note.dup.to_s
        note=note.gsub(/^([\d]+)\s+/,'^~\1 ').
          gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,
            ' \\\\\\ ')
        n << note
      end
      notes_a=n.flatten
      doc_obj_txt=doc_obj_txt.
        gsub(/#{Mx[:en_a_o]}([\d]+)\s+(?:.+?)#{Mx[:en_a_c]}/,'~^')   # endnote marker marked up
    else
      doc_obj_txt=doc_obj_txt.
        gsub(/#{Mx[:en_b_o]}[\d]+\s+(.+?)#{Mx[:en_b_c]}/,
          '~[ \1 ]~').     # inline endnote with marker marked up
        gsub(/#{Mx[:en_a_o]}([*+]+)\s+(.+?)#{Mx[:en_a_c]}/,
          '~{\1 \2 }~'). # inline endnote with marker marked up
        gsub(/#{Mx[:en_b_o]}([*+]+)\s+(.+?)#{Mx[:en_b_c]}/,
          '~[\1 \2 ]~') # inline endnote with marker marked up
    end
    [doc_obj_txt,notes_a]
  end
  def objects #def i_ao_o_src_markup_restore(txtobj)
    def code_(dob)
      if dob.is==:code
        dob.obj=dob.obj.gsub(/(^|[^}])_([<>])/m,'\1\2'). # _> _<
          gsub(/(^|[^}])_([<>])/m,'\1\2') # _<_<
      end
      dob
    end
    def block_(dob)
      dob.obj=if dob.of==:block                                   # watch
        dob.obj.gsub(/#{Mx[:gl_o]}●#{Mx[:gl_c]}/,"* ").
          gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,"\n")
      else dob.obj.gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,"\n\n")
      end
      dob
    end
    def textface_marks_po4a(dob,endnotes_=:inline)
      notes=''
      dob.obj=dob.obj.
        gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,
          Mx[:src_bold_o] + '\1' + Mx[:src_bold_c]).
        gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,
          Mx[:src_italics_o] + '\1' + Mx[:src_italics_c]).
        gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,
          Mx[:src_underscore_o] + '\1' + Mx[:src_underscore_c]).
        gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,
          Mx[:src_subscript_o] + '\1' + Mx[:src_subscript_c]).
        gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,
          Mx[:src_superscript_o] + '\1' + Mx[:src_superscript_c]).
        gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,
          Mx[:src_insert_o] + '\1' + Mx[:src_insert_c]).
        gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,
          Mx[:src_cite_o] + '\1' + Mx[:src_cite_c]).
        gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,
          Mx[:src_strike_o] + '\1' + Mx[:src_strike_c]).
        gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,
          Mx[:src_monospace_o] + '\1' + Mx[:src_monospace_c])
      unless dob.is==:code
        dob.obj=dob.obj.
          gsub(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,'\1').
          gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'\1').
          gsub(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,
            '\1 [link: <\2>]').
          gsub(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}image/,
            '\1 [link: local image]').
          gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,'\1')
        dob.obj,notes=extract_endnotes(dob.obj,endnotes_)
        dob.obj=dob.obj.
          gsub(/#{Mx[:gl_o]}(?:#lt|#060)#{Mx[:gl_c]}/,'<').
          gsub(/#{Mx[:gl_o]}(?:#gt|#062)#{Mx[:gl_c]}/,'>').
          gsub(/#{Mx[:gl_o]}#(?:038|amp)#{Mx[:gl_c]}/,'&').
          gsub(/#{Mx[:gl_o]}#033#{Mx[:gl_c]}/,'!').
          gsub(/#{Mx[:gl_o]}#035#{Mx[:gl_c]}/,'#').
          gsub(/#{Mx[:gl_o]}#042#{Mx[:gl_c]}/,'*').
          gsub(/#{Mx[:gl_o]}#045#{Mx[:gl_c]}/,'-').
          gsub(/#{Mx[:gl_o]}#047#{Mx[:gl_c]}/,'/').
          gsub(/#{Mx[:gl_o]}#095#{Mx[:gl_c]}/,'_').
          gsub(/#{Mx[:gl_o]}#123#{Mx[:gl_c]}/,'{').
          gsub(/#{Mx[:gl_o]}#125#{Mx[:gl_c]}/,'}').
          gsub(/#{Mx[:gl_o]}#126#{Mx[:gl_c]}/,'~').
          gsub(/#{Mx[:gl_o]}#169#{Mx[:gl_c]}/,'©')
      end
      dob=block_(dob)
      dob=code_(dob)
      dob.obj=dob.obj.gsub(/#{Mx[:br_page]}\s*|#{Mx[:br_page_new]}/,''). # remove page breaks
        gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'\1').
        gsub(/#{Mx[:mk_o]}:name#(\S+?)#{Mx[:mk_c]}/,'').                 # remove name links
        gsub(/&nbsp;|#{Mx[:nbsp]}/,' ').                                 # decide on
        gsub(/(?:^|[^_\\])#{Mx[:lnk_o]}(\S+?\.(?:png|jpg|gif)) .+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,
          '    [ \1 ]'). #"[ #{dir.url.images_local}\/\\1 ]")
        gsub(/(?:^|[^_\\])#{Mx[:lnk_o]}(\S+?\.(?:png|jpg|gif)) .+?#{Mx[:lnk_c]}image/,
          '    [ \1 ]'). #"[ #{dir.url.images_local}\/\\1 ]")
        gsub(/(?:^|[^_\\])\{\s*\S+?\.(?:png|jpg|gif)\s+.+?"(.*?)"\s*\}\S+/,
          '[image: "\1"]')
      [dob,notes]
    end
    def object_marks
      @txtobj
    end
    self
  end
end
__END__
#+END_SRC

* document header

#+NAME: sisu_document_header
#+BEGIN_SRC emacs-lisp
<<./sisu_version_info_and_doc_header_including_copyright_and_license.org:sisu_doc_header_including_copyright_and_license()>>
#+END_SRC