f100b2d84775e75b322bde3f457e978fd41ee45c
[software/sisu] / lib / sisu / develop / utils.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** system environment, debug related
8
9 ** Author: Ralph Amissah
10 <ralph@amissah.com>
11 <ralph.amissah@gmail.com>
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see <http://www.gnu.org/licenses/>.
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 <http://www.fsf.org/licensing/licenses/gpl.html>
39 <http://www.gnu.org/licenses/gpl.html>
40
41 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/utils.rb;hb=HEAD>
55
56 =end
57 module SiSU_Utils
58 class CodeMarker
59 def initialize(line,file,color=:red)
60 @line,@file,@color=line.to_s,file,color
61 end
62 def ansi(color=nil)
63 @color=color ? color : @color
64 c={}
65 c[:on]=case @color
66 when :red then ANSI_C[:red]
67 when :green then ANSI_C[:green]
68 when :yellow then ANSI_C[:yellow]
69 when :blue then ANSI_C[:blue]
70 when :fuchsia then ANSI_C[:fuchsia]
71 when :cyan then ANSI_C[:cyan]
72 when :inv_red then ANSI_C[:inv_red]
73 when :inv_green then ANSI_C[:inv_green]
74 when :inv_yellow then ANSI_C[:inv_yellow]
75 when :inv_blue then ANSI_C[:inv_blue]
76 when :inv_fuchsia then ANSI_C[:inv_fuchsia]
77 when :inv_cyan then ANSI_C[:inv_cyan]
78 when :b_red then ANSI_C[:b_red]
79 when :b_green then ANSI_C[:b_green]
80 when :b_yellow then ANSI_C[:b_yellow]
81 when :b_blue then ANSI_C[:b_blue]
82 when :b_fuchsia then ANSI_C[:b_fuchsia]
83 when :b_cyan then ANSI_C[:b_cyan]
84 else ANSI_C[:red]
85 end
86 c[:off]= ANSI_C[:off]
87 #ansi_color + @line.to_s + ansi_color_off + ' ' + @file.gsub(/([^\/]+$)/,"#{ansi_color}\\1#{ansi_color_off}")
88 c
89 end
90 def var(v,x)
91 h={ c: nil, m: '' }
92 if v.is_a?(Hash)
93 h[:c] = (defined? v[:c]) ? v[:c] : ''
94 h[:m] = (defined? v[:m]) ? v[:m] : ''
95 elsif (v.is_a?(Symbol) \
96 and x.is_a?(String))
97 if v.is_a?(Symbol)
98 h[:c]=v
99 elsif v.is_a?(String)
100 h[:m]=v
101 end
102 if x.is_a?(String)
103 h[:m]=x
104 end
105 elsif (v.is_a?(Symbol) \
106 or v.is_a?(String))
107 if v.is_a?(Symbol)
108 h[:c]=v
109 elsif v.is_a?(String)
110 h[:m]=v
111 end
112 end
113 h[:c] = (defined? h[:c]) ? h[:c] : ''
114 h[:m] = (defined? h[:m]) ? h[:m] : ''
115 h[:c]=ansi(h[:c])
116 h[:m]=message(h[:m])
117 h
118 end
119 def message(msg='')
120 @message=(msg.nil? || msg.empty?) \
121 ? ''
122 : ' ' + msg
123 end
124 def set(v=nil,x=nil)
125 v=var(v,x)
126 file,path=File.basename(@file),File.dirname(@file)
127 v[:c][:on] + @line + v[:c][:off] + ' ' \
128 + path + '/' \
129 + "#{v[:c][:on]}#{file}#{v[:c][:off]}" \
130 + v[:m]
131 end
132 def mark(v=nil,x=nil)
133 puts set(v,x)
134 end
135 def ok(v=nil,x=nil)
136 if (v.is_a?(Symbol) \
137 and x.is_a?(String))
138 x= '*OK* ' + x
139 elsif v.is_a?(String)
140 v='*OK* ' + v
141 x=nil
142 else
143 v,x='*OK*',nil
144 end
145 puts set(v,x)
146 end
147 def warn(v=nil,x=nil)
148 if (v.is_a?(Symbol) \
149 and x.is_a?(String))
150 x="\n " + '*WARN* ' + x
151 elsif v.is_a?(String)
152 v="\n " + '*WARN* ' + v
153 x=nil
154 else
155 v,x='*WARN*',nil
156 end
157 puts set(v,x)
158 end
159 def error(v=nil,x=nil)
160 if (v.is_a?(Symbol) \
161 and x.is_a?(String))
162 x="\n " + '*ERROR* ' + x
163 elsif v.is_a?(String)
164 v="\n " + '*ERROR* ' + v
165 x=nil
166 else
167 v,x='*ERROR*',nil
168 end
169 STDERR.puts set(v,x)
170 end
171 end
172 class Path
173 def initialize(dir=Dir.pwd)
174 @dir=dir
175 end
176 def base_markup(call_path=nil)
177 call_path = call_path \
178 ? call_path \
179 : Dir.pwd
180 (/(\S+?)(?:\/(?:#{Px[:lng_lst_rgx]}))?$/).match(call_path)[1]
181 end
182 def base_markup_stub
183 m=/.+\/(?:src\/)?(\S+)/im
184 base_markup[m,1]
185 end
186 def image_src
187 if base_markup =~/sisupod\/doc[\/]?$/
188 base_markup.gsub(/\/doc[\/]?$/,'/image')
189 elsif FileTest.directory?("#{base_markup}/_sisu/image")
190 "#{base_markup}/_sisu/image"
191 end
192 end
193 def bmd
194 base_markup
195 end
196 end
197 end
198 __END__
199 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark({ m: %{
200 code: #{@@flag[:code]}, <-- close "#{t_o}"
201 }})
202 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark({ m: %{
203 code: #{@@flag['code']}, <-- close "#{t_o}"
204 }, c: :green})
205 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark({ m: %{
206 code: #{@@flag['code']}, open --> "#{t_o}"
207 }}) if t_o=~/^```/m
208 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark(%{ code: #{@@flag['code']}, open --> "#{t_o}" }) \
209 if t_o=~/^```/m
210 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark("open -->")
211 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).mark("open -->")
212 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark("open -->",:green)
213 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark(:green,"open -->")
214 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark("open -->")
215 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark(:green)
216 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark
217 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).mark("open -->")
218 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).mark('open -->',:green)
219 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:red).mark
220 puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:red).set
221 puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).set('',:green)
222 puts SiSU_Utils::CodeMarker.new(__LINE__,__FILE__).set('',:fuchsia)
223 puts ANSI_C[:red] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__
224 puts "#{ANSI_C[:red]} #{__LINE__.to_s} #{ANSI_C[:off]} #{__FILE__}"
225 puts ANSI_C[:fuchsia] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__.gsub(/([^\/]+$)/,"#{ANSI_C[:fuchsia]}\\1#{ANSI_C[:off]}")
226 puts ANSI_C[:red] + __LINE__.to_s + ANSI_C[:off] + ' ' + __FILE__.gsub(/([^\/]+$)/,"#{ANSI_C[:red]}\\1#{ANSI_C[:off]}")