File: jfold.rb

package info (click to toggle)
liboptparse-ruby 0.8.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 428 kB
  • ctags: 644
  • sloc: ruby: 1,819; makefile: 47
file content (48 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download | duplicates (2)
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
# $Id: junfold.rb,v 1.2 2001/04/13 06:20:49 nobu Exp $

module RD
  module Japanese
    UNFOLD = {
      "iso-2022-jp" => /\e\(B\n\s*\e\$B/n,
      "euc-jp" => /((?>(?:[\x8e\xa1-\xfe][\xa1-\xfe])+))\n\s*([\x8e\xa1-\xfe])/n,
      "shift_jis" => /((?>(?:[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]|[\xa0-\xdf])+))\n\s*([\x81-\xfc])/n
    }

    def unfold(str, charset = @charset)
      if pat = UNFOLD[charset]
	str.gsub(pat, '\1\2')
      else
	str
      end
    end

    FOLD = {
      "iso-2022-jp" => [/\e\$B(?:[\x21-\x7f]{2})*(?:\x21\x23)+(?!$)/n, "\\&\n\e(B"],
      "euc-jp" => [/(?>(?!\xa1\xa3)[\x8e\xa1-\xfe][\xa1-\xfe])+(?:\xa1\xa3)+(?!$)/n],
      "shift_jis" => [/(?>(?!\x81\x42)(?:[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]|[\xa0-\xdf]))+(?:\x81\x42)+(?!$)/n]
    }

    def fold(str, charset = @charset)
      if pat = FOLD[charset]
	pat, rep = pat
	str.gsub(pat, rep || "\\&\n")
      else
	str
      end
    end
  end

  class RD2HTMLVisitor < RDVisitor
    include Japanese

    %w[ItemListItem EnumListItem Footnote TextBlock].each do |el|
      el = "apply_to_#{el}"
      eval %{
	alias _#{el} #{el}
	def #{el}(element, content)
	  _#{el}(element, [fold(unfold(content.join("")))])
	end
      }
    end
  end
end