aboutsummaryrefslogtreecommitdiffhomepage
path: root/data/sisu/v3/conf/convert/kdissert_to_sisu
blob: 35921c3eb6959056fa8b37f7aa0fcedff163f29b (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
#!/usr/bin/env ruby
=begin
 * Name: modify.rb
 * Author: Ralph Amissah
   * http://www.jus.uio.no/sisu
   * http://www.jus.uio.no/sisu/SiSU/download
 * Description: Conversion script from kdissert .kdi to sisu markup .sst
 * Copyright (C) 2004, 2006 Ralph Amissah
 * Packaged with: SiSU information Structuring Universe -
     Structured information, Serialized Units
 * SiSU Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ralph Amissah

 * License: GPL 3 or later

  Summary of GPL 3

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 3 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  If you have Internet connection, the latest version of the GPL should be
  available at these locations:
    http://www.fsf.org/licenses/gpl.html
    http://www.gnu.org/copyleft/gpl.html
    http://www.jus.uio.no/sisu/gpl3.fsf

  © Ralph Amissah 1997, current 2006.
  All Rights Reserved.

 * Ralph Amissah: ralph@amissah.com
                  ralph.amissah@gmail.com
=end
require 'rexml/document'
include REXML
argv=$*
@sisu,@sisu_base=Array.new,Array.new
unless argv.empty?
  argv.each do |kdi|
    if kdi =~/\.kdi$/
      system("cp #{kdi} #{kdi}.tar.gz && tar xzvf #{kdi}.tar.gz && rm #{kdi}.tar.gz")
      file=File.new("maindoc.xml")
      #system("cp #{kdi} /tmp/. && cd /tmp && tar xzvf /tmp/#{kdi} && cd -")
      #file=File.new("/tmp/maindoc.xml")
      @output=File.new("#{kdi}.sst",'w')
      doc=Document.new(file)
      root=doc.root
      @el=Array.new
      root.each do |x|
      end
      root.each_with_index do |content,idx|
        if root.elements["item[#{idx}]/summary"]
          id,ma,ch=nil,nil,Array.new
          if root.elements["item[#{idx}]/id"]
            id=root.elements["item[#{idx}]/id"].text.to_i
          end
          if root.elements["item[#{idx}]/parent"]
            ma=root.elements["item[#{idx}]/parent"].text.to_s
          end
          if root.elements["item[#{idx}]/child"] #problem only get one child, even where several
            root.get_elements("item[#{idx}]/child").each do |x|
              ch << x.text
            end
          end
          if root.elements["item[#{idx}]/summary"]
            sum=root.elements["item[#{idx}]/summary"].text.to_s.strip
          end
          if root.elements["item[#{idx}]/text"]
            txt=root.elements["item[#{idx}]/text"].text.to_s.strip
          end
          @el[id]=Hash.new
          @el[id][:id]=id
          @el[id][:ma]=ma
          @el[id][:ch]=ch
          @el[id][:sum]=sum
          @el[id][:txt]=txt
          if ma == '-1'
            @el[id][:lev]=':A'
            @top=id
          end
        end
      end
      @doc=Array.new
      @title=@el[@top][:sum]
      p @el[@top][:sum]
      @doc << ':A~ ' + @el[@top][:sum]
      @el[@top][:ch].each do |x|
        @el[x.to_i][:lev]='1'
        @doc << '1~ ' + @el[x.to_i][:sum]
        @doc << @el[x.to_i][:txt]
        @el[x.to_i][:ch].each do |y|
          @el[y.to_i][:lev]='2'
          @doc << '2~ ' + @el[y.to_i][:sum]
          @doc << @el[y.to_i][:txt]
          @el[y.to_i][:ch].each do |z|
            @el[z.to_i][:lev]='3'
            @doc << '3~ ' + @el[z.to_i][:sum]
            @doc << @el[z.to_i][:txt]
            @el[z.to_i][:ch].each do |za|  #unsupported... consder
              @el[za.to_i][:lev]='4'
              @doc << '!_ ' + @el[za.to_i][:sum]
              @doc << @el[za.to_i][:txt]
            end
          end
        end
      end
      #@el.each_with_index do |x,i|
      #  if x: puts "#{i}: lev: #{x[:lev]}, id: #{x[:id]}, ma: #{x[:ma]}, ch: #{x[:ch].join(',')}"
      #  end
      #end
# regexs strip most kdissert markup, and provide minimal info for sisu markup -->
      @doc.each do |c,idx|
        c.gsub!(/<\/summary>/,'')
        c.gsub!(/<html>.+?\n|<\/body>|<\/html>|<p>/m,'')
        c.gsub!(/<\/p>/,"\n")
        c.gsub!(/<span style="font-weight:600">(.+?)<\/span>/," *{ \\1 }* ")
        c.gsub!(/<span style="text-decoration:underline">(.+?)<\/span>/," _{ \\1 }_ ")
        c.gsub!(/<span style="font-style:italic">(.+?)<\/span>/," /{ \\1 }/ ")
        c.gsub!(/<ul type="disc">/,'_* ')
        c.gsub!(/<\S+?>/,'')
        c.gsub!(/&lt;(:p[bn])&gt;/,"<\\1>")
        c.gsub!(/&lt;&lt;(|.?|@|.?|)/,"<<\\1")
        c.gsub!(/&quot;/,'"')
        c.gsub!(/ \s+/,' ')
        @sisu_base << c.strip + "\n\n"
      end
    else puts ".kdi extension expected, filename not recognised: << #{kdi} >>"
    end
  end
else puts '.kdi file for conversion to sisu expected'
end
sisu_head=<<WOK
% converted from Kdissert to SiSU

@title: #@title

@prefix: Kdissert mind/topic mapping auto conversion to SiSU markup

@links: {Kdissert}http://freehackers.org/~tnagy/kdissert/

@base_program: kdissert

% @date:

@rcs: $Id$

@level: num_top=1

WOK
#puts @sisu_base
@output << sisu_head << @sisu_base
__END__