Permalink
Browse files

import from jp.rubyist.net

  • Loading branch information...
1 parent 991ec38 commit f39531efd9191a4ef67019f8c57d74447d513989 @hsbt hsbt committed Mar 22, 2013
Showing with 1,227 additions and 0 deletions.
  1. +751 −0 ansisys.rb
  2. +96 −0 attach_color_keywords.rb
  3. +9 −0 attach_expandimg.rb
  4. +70 −0 attach_html.rb
  5. +62 −0 attach_pre.rb
  6. +20 −0 backnumber.rb
  7. +11 −0 clear-float.rb
  8. +151 −0 hikidoc_pre.rb
  9. +57 −0 html_tags.rb
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,96 @@
+# attach_color_keywords.rb - Hiki plugin to highlight a keyword with a color
+#
+# Installation:
+# Copy or symlink this file into misc/plugin, and activate this
+# plugin and attach plugin from configuration interface
+#
+# Usage:
+# Upload a text file using attach plugin and write the
+# following code in the page:
+# {{attach_color_keywords(FILENAME, 'KEYWORD', COLOR)}}
+#
+# FILENAME: name of the uploaded file
+# KEYWORD: keyword(s) to be highlighted
+# COLOR: color name of color code
+#
+# Copyright (C) 2007 zunda <zunda at freeshell.org>
+#
+# Permission is granted for use, copying, modification, distribution,
+# and distribution of modified versions of this work as long as the
+# above copyright notice is included.
+#
+
+def attach_color_keywords(file_name, keyword, color, page = @page)
+ return '' unless file_name =~ /\.(txt|rd|rb|c|pl|py|sh|java|html|htm|css|xml|xsl|sql|yaml)\z/i
+ page_file_name = "#{page.untaint.escape}/#{file_name.untaint.escape}"
+ path = "#{@conf.cache_path}/attach/#{page_file_name}"
+ unless File.exists?(path)
+ raise PluginError, "No such file:#{page_file_name}"
+ end
+ src = File.open(path){|f| f.read}
+ pre_color_keywords(src, {keyword => color}, @options)
+end
+
+def pre_color_keywords(string, colors = {}, options = {})
+ tabstop = ' ' * (options['attach.tabstop'] ? options['attach.tabstop'].to_i : 2)
+
+ unless colors.keys.empty?
+ span_string = ''
+ remaining = string.gsub(/(.*?)(\b#{Regexp.union(*colors.keys)}\b)/m) do
+ prefix = $1
+ key = $2
+ span_string += prefix.escapeHTML
+ span_string += %Q|<span style="color:#{colors[key].escapeHTML}">#{key.escapeHTML}</span>|
+ ''
+ end
+ span_string += remaining.escapeHTML
+ else
+ span_string = string.escapeHTML
+ end
+
+ '<pre>' + span_string.gsub(/^\t+/){|t| tabstop * t.size}.to_euc + '</pre>'
+end
+
+if __FILE__ == $0
+ require 'test/unit'
+ require 'cgi'
+ require 'nkf'
+
+ class String
+ def escapeHTML
+ CGI::escapeHTML(self)
+ end
+
+ def to_euc
+ NKF::nkf('-m0 -e', self)
+ end
+ end
+
+ class TestPreColorKeywords < Test::Unit::TestCase
+ def test_simple
+ assert_equal('<pre>test</pre>', pre_color_keywords('test'))
+ end
+
+ def test_escape
+ assert_equal('<pre>&lt;test&gt;</pre>', pre_color_keywords('<test>'))
+ end
+
+ def test_red_pill
+ assert_equal('<pre><span style="color:red">red</span> pill</pre>',
+ pre_color_keywords('red pill', {'red' => 'red'}))
+ end
+
+ def test_take_the_red_pill
+ assert_equal('<pre>Take the <span style="color:red">red</span> pill</pre>',
+ pre_color_keywords('Take the red pill', {'red' => 'red'}))
+ end
+
+ def test_pill_makes_ill
+ assert_equal('<pre>pill makes <span style="color:purple">ill</span>.</pre>', pre_color_keywords('pill makes ill.', {'ill' => 'purple'}))
+ end
+
+ def test_color_escape
+ assert_equal('<pre><span style="color:&quot;bad&quot;">bad</span></pre>', pre_color_keywords('bad', {'bad' => '"bad"'}))
+ end
+ end
+end
View
@@ -0,0 +1,9 @@
+def attach_expandimg(file1,file2=nil,page=@page)
+ if(file2.nil?)
+ attach_image_anchor(file1,page)
+ else
+ s = %Q!<a href="!
+ s << %Q!#{@conf.cgi_name}#{cmdstr('plugin', "plugin=attach_download;p=#{page.escape};file_name=#{file2.escape}")}">!
+ s << %Q!#{attach_image_anchor(file1,page)}</a>!
+ end
+end
View
@@ -0,0 +1,70 @@
+def attach_html(file_name, start_no=1, page=@page)
+ tabstop = ' ' * (@options['attach.tabstop'] ? @options['attach.tabstop'].to_i : 2)
+
+ file = "#{@cache_path}/attach/#{page.untaint.escape}/#{file_name.untaint.escape}"
+ code = File::readlines(file).join.gsub(/^\t+/) {|t| tabstop * t.size}.to_euc.sub(/\n\z/,'')
+ s = ""
+ s << %Q!<pre>!
+ line = start_no !=- 1 ? start_no : 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ code.scan(/(\{\{.+?\}\})|(<%.+?%>)|(<!.+?>)|(<.+?>)|(&[a-zA-Z\#0-9]+;?)|([^<{\n]+)|(\n)|./m) {|str|
+ m = $~
+ if m[1] # Web::Template
+ str = CGI::escapeHTML(m[0])
+ str.each {|x|
+ s << "<span class=\"html_webtemplate\">" << x << "</span>"
+ if x=~/\n\z/
+ line += 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ elsif m[2] # ERb
+ str = CGI::escapeHTML(m[0])
+ str.each {|x|
+ s << "<span class=\"html_erb\">" << x << "</span>"
+ if x=~/\n\z/
+ line += 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ elsif m[3] # comment
+ str = CGI::escapeHTML(m[0])
+ str.each {|x|
+ s << "<span class=\"html_comment\">" << x << "</span>"
+ if x=~/\n\z/
+ line += 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ elsif m[4] # tag
+ str = CGI::escapeHTML(m[0])
+ tag_m = str.match(/&lt;\/?([a-z0-9]+)/i)
+ if tag_m[1]=~/^(?:html|head|body|p|h[1-6]|ul|ol|pre|dl|div|blockquote|form|hr|table)/i
+ str.each {|x|
+ s << "<span class=\"html_tag_block\">" << x << "</span>"
+ if x=~/\n\z/
+ line += 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ else
+ str.each {|x|
+ s << "<span class=\"html_tag\">" << x << "</span>"
+ if x=~/\n\z/
+ line += 1
+ s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ end
+ elsif m[5] # charref
+ s << "<span class=\"html_charref\">" << CGI::escapeHTML(m[0]) << "</span>"
+ elsif m[6] # text
+ s << CGI::escapeHTML(m[0])
+ elsif m[7] # lf
+ line += 1
+ s << "\n<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
+ end
+ }
+ s << %Q!</pre>!
+ s
+end
View
@@ -0,0 +1,62 @@
+## Usage: attach_pre('file.html', 'page=foo&linenum=1&inline=true')
+def attach_pre(filename, option_str='page=nil&amp;linenum=nil&amp;inline=false')
+ # parse option string
+ #$stderr.puts "*** debug: option_str=#{option_str.inspect}"
+ page = @page
+ linenum = nil
+ inline = false
+ if option_str
+ options = {}
+ option_str.split(/\&amp;/).each do |option|
+ if option =~ /\A(\w+)=(.*)/
+ key = $1; val = $2
+ case val
+ when 'true', 'yes' ; val = true
+ when 'false', 'no' ; val = false
+ when 'nil', 'null' ; val = nil
+ when /\A\d+\z/ ; val = val.to_i
+ end
+ else
+ key = option; val = true
+ end
+ options[key] = val
+ end
+ #$stderr.puts "*** debug: options=#{options.inspect}"
+ v = options['page']; page = v if v.is_a?(String)
+ v = options['linenum']; linenum = v if v.is_a?(Integer) || v == nil
+ v = options['inline']; inline = v if v == true || v == false
+ end
+
+ #if filename =~ /\.(txt|rd|rb|c|pl|py|sh|java|html|htm|css|xml|xsl|sql|yaml|rhtml|xhtml|php|eruby)\z/i
+
+ # read file content
+ filepath = "#{@conf.cache_path}/attach/#{page.untaint.escape}/#{filename.untaint.escape}"
+ content = File.open(filepath) { |f| f.read() }
+
+ # add line number, expand tab character, and escape HTML character
+ s = ''
+ n = linenum.is_a?(Integer) ? linenum - 1 : 0
+ content.each_line do |line|
+ s << "%03d| " % (n += 1) if linenum # add line number
+ s << line.gsub(/([^\t]{8})|([^\t]*)\t/n) { [$+].pack("A8") } # expand tab
+ end
+ s = s.escapeHTML.to_euc
+
+ # inline expantion
+ if inline
+ # '{{*...*}}' => '<strong>...</strong>'
+ s.gsub!(/\{\{\*/, '<strong>')
+ s.gsub!(/\*\}\}/, '</strong>')
+ # '{{/.../}}' => '<em>...</em>'
+ s.gsub!(/\{\{\//, '<em>')
+ s.gsub!(/\/\}\}/, '</em>')
+ end
+
+ # return value
+ "<pre>#{s}</pre>"
+
+ #end
+end
+
+
+
View
@@ -0,0 +1,20 @@
+def indices(re)
+ re = Regexp.compile(re.gsub(/(?!\\)\#/, '\#'))
+ str = "<ul>\n"
+ @db.pages.sort.each do |page|
+ if re =~ page
+ if page == @page
+ str << "<li><strong>#{page_name(page).escapeHTML}</strong>\n"
+ else
+ str << "<li>#{hiki_anchor(page.escape, page_name(page))}\n"
+ end
+ end
+ end
+ str << "</ul>\n"
+end
+
+def backnumber(title)
+ indices("\\A[0-9]{4}-#{title}\\z")
+end
+
+export_plugin_methods(:backnumber, :indices)
View
@@ -0,0 +1,11 @@
+def clear_float(position=nil)
+ case position
+ when /^\s*l/i
+ "<br style='clear:left;'>"
+ when /^\s*r/i
+ "<br style='clear:right;'>"
+ else
+ "<br style='clear:both;'>"
+ end
+end
+export_plugin_methods(:clear_float)
Oops, something went wrong.

0 comments on commit f39531e

Please sign in to comment.