aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/utils.rb
blob: a52324363a6aa458d962f1a38e9485d472efe71a (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
# encoding: utf-8
=begin

* Name: SiSU

** Description: documents, structuring, processing, publishing, search
*** system environment, debug related

** Author: Ralph Amissah
  <ralph@amissah.com>
  <ralph.amissah@gmail.com>

** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
  All Rights Reserved.

** License: GPL 3 or later:

  SiSU, a framework for document structuring, publishing and search

  Copyright (C) Ralph Amissah

  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, see <http://www.gnu.org/licenses/>.

  If you have Internet connection, the latest version of the GPL should be
  available at these locations:
  <http://www.fsf.org/licensing/licenses/gpl.html>
  <http://www.gnu.org/licenses/gpl.html>

  <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

** SiSU uses:
  * Standard SiSU markup syntax,
  * Standard SiSU meta-markup syntax, and the
  * Standard SiSU object citation numbering and system

** Hompages:
  <http://www.jus.uio.no/sisu>
  <http://www.sisudoc.org>

** Git
  <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
  <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/utils.rb;hb=HEAD>

=end
module SiSU_Utils
  class CodeMarker
    def initialize(line,file,color=:red)
      @line,@file,@color=line.to_s,file,color
    end
    def ansi(color=nil)
      @color=color ? color : @color
      c={}
      c[:on]=case @color
      when :red         then ANSI_C[:red]
      when :green       then ANSI_C[:green]
      when :yellow      then ANSI_C[:yellow]
      when :blue        then ANSI_C[:blue]
      when :fuchsia     then ANSI_C[:fuchsia]
      when :cyan        then ANSI_C[:cyan]
      when :inv_red     then ANSI_C[:inv_red]
      when :inv_green   then ANSI_C[:inv_green]
      when :inv_yellow  then ANSI_C[:inv_yellow]
      when :inv_blue    then ANSI_C[:inv_blue]
      when :inv_fuchsia then ANSI_C[:inv_fuchsia]
      when :inv_cyan    then ANSI_C[:inv_cyan]
      when :b_red       then ANSI_C[:b_red]
      when :b_green     then ANSI_C[:b_green]
      when :b_yellow    then ANSI_C[:b_yellow]
      when :b_blue      then ANSI_C[:b_blue]
      when :b_fuchsia   then ANSI_C[:b_fuchsia]
      when :b_cyan      then ANSI_C[:b_cyan]
      else                   ANSI_C[:red]
      end
      c[:off]= ANSI_C[:off]
      #ansi_color + @line.to_s + ansi_color_off + ' ' + @file.gsub(/([^\/]+$)/,"#{ansi_color}\\1#{ansi_color_off}")
      c
    end
    def var(v,x)
      h={ c: nil, m: '' }
      if v.is_a?(Hash)
        h[:c] = (defined? v[:c]) ? v[:c] : ''
        h[:m] = (defined? v[:m]) ? v[:m] : ''
      elsif (v.is_a?(Symbol) \
      and x.is_a?(String))
        if v.is_a?(Symbol)
          h[:c]=v
        elsif v.is_a?(String)
          h[:m]=v
        end
        if x.is_a?(String)
          h[:m]=x
        end
      elsif (v.is_a?(Symbol) \
      or v.is_a?(String))
        if v.is_a?(Symbol)
          h[:c]=v
        elsif v.is_a?(String)
          h[:m]=v
        end
      end
      h[:c] = (defined? h[:c]) ? h[:c] : ''
      h[:m] = (defined? h[:m]) ? h[:m] : ''
      h[:c]=ansi(h[:c])
      h[:m]=message(h[:m])
      h
    end
    def message(msg='')
      @message=(msg.nil? || msg.empty?) \
      ? ''
      : ' ' + msg
    end
    def set(v=nil,x=nil)
      v=var(v,x)
      file,path=File.basename(@file),File.dirname(@file)
      v[:c][:on] + @line + v[:c][:off] + ' ' \
      + path + '/' \
      + "#{v[:c][:on]}#{file}#{v[:c][:off]}" \
      + v[:m]
    end
    def mark(v=nil,x=nil)
      puts set(v,x)
    end
    def tell(v=nil,x=nil)
      puts set(v,x)
    end
    def report(v=nil,x=nil)
      puts set(v,x)
    end
    def ok(v=nil,x=nil)
      if (v.is_a?(Symbol) \
      and x.is_a?(String))
        x= '*OK* ' + x
      elsif v.is_a?(String)
        v='*OK* ' + v
        x=nil
      else
        v,x='*OK*',nil
      end
      puts set(v,x)
    end
    def warn(v=nil,x=nil)
      if (v.is_a?(Symbol) \
      and x.is_a?(String))
        x="\n  " + '*WARN* ' + x
      elsif v.is_a?(String)
        v="\n  " + '*WARN* ' + v
        x=nil
      else
        v,x='*WARN*',nil
      end
      puts set(v,x)
    end
    def error(v=nil,x=nil)
      if (v.is_a?(Symbol) \
      and x.is_a?(String))
        x="\n  " + '*ERROR* ' + x
      elsif v.is_a?(String)
        v="\n  " + '*ERROR* ' + v
        x=nil
      else
        v,x='*ERROR*',nil
      end
      STDERR.puts set(v,x)
    end
  end
  class Path
    def initialize(dir=Dir.pwd)
      @dir=dir
    end
    def base_markup(call_path=nil)
      call_path = call_path \
      ? call_path \
      : Dir.pwd
      (/(\S+?)(?:\/(?:#{Px[:lng_lst_rgx]}))?$/).match(call_path)[1]
    end
    def base_markup_stub
      m=/.+\/(?:src\/)?(\S+)/im
      base_markup[m,1]
    end
    def image_src
      if base_markup =~/sisupod\/doc[\/]?$/
        base_markup.gsub(/\/doc[\/]?$/,'/image')
      elsif FileTest.directory?("#{base_markup}/_sisu/image")
        "#{base_markup}/_sisu/image"
      end
    end
    def bmd
      base_markup
    end
  end
end
__END__
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark({ m: %{
  code: #{@@flag[:code]}, <-- close "#{t_o}"
}})
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark({ m: %{
  code: #{@@flag['code']}, <-- close "#{t_o}"
}, c: :green})
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark({ m: %{
  code: #{@@flag['code']}, open --> "#{t_o}"
}}) if t_o=~/^```/m
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark(%{ code: #{@@flag['code']}, open --> "#{t_o}" }) \
if t_o=~/^```/m
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark("open -->")
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).mark("open -->")
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark("open -->",:green)
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark(:green,"open -->")
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark("open -->")
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark(:green)
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark("open -->")
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark('open -->',:green)
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:red).mark
puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:red).set
puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).set('',:green)
puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).set('',:fuchsia)
puts ANSI_C[:red] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__
puts "#{ANSI_C[:red]} #{__LINE__.to_s} #{ANSI_C[:off]} #{__FILE__}"
puts ANSI_C[:fuchsia] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__.gsub(/([^\/]+$)/,"#{ANSI_C[:fuchsia]}\\1#{ANSI_C[:off]}")
puts ANSI_C[:red] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__.gsub(/([^\/]+$)/,"#{ANSI_C[:red]}\\1#{ANSI_C[:off]}")