| [2107] | 1 | |
|---|
| 2 | class WikiForme::BlockElement |
|---|
| 3 | |
|---|
| 4 | # 包含可能要素の定義 |
|---|
| 5 | def self.contain(*names) |
|---|
| 6 | const_get(:CONTAIN_NAMES).replace( names.map{|n| n.to_sym} ) |
|---|
| 7 | end |
|---|
| 8 | def self.uncontain(*names) |
|---|
| 9 | const_get(:UNCONTAIN_NAMES).replace( names.map{|n| n.to_sym} ) |
|---|
| 10 | end |
|---|
| 11 | def self.add_contain(*names) |
|---|
| 12 | const_get(:CONTAIN_NAMES).concat( names.map{|n| n.to_sym} ) |
|---|
| 13 | end |
|---|
| 14 | def self.add_uncontain(*names) |
|---|
| 15 | const_get(:UNCONTAIN_NAMES).concat( names.map{|n| n.to_sym} ) |
|---|
| 16 | end |
|---|
| 17 | |
|---|
| 18 | # 所属グループの定義 |
|---|
| 19 | def self.group(*names) |
|---|
| 20 | const_get(:GROUP_NAMES).replace( names.map{|n| n.to_sym} ) |
|---|
| 21 | end |
|---|
| 22 | def self.add_group(*names) |
|---|
| 23 | const_get(:GROUP_NAMES).concat( names.map{|n| n.to_sym} ) |
|---|
| 24 | end |
|---|
| 25 | |
|---|
| 26 | # 親要素の定義 |
|---|
| 27 | def self.parent(name) |
|---|
| 28 | const_get(:PARENT_COMPLETION_NAME).replace(name.to_sym) |
|---|
| 29 | end |
|---|
| 30 | def self.parent=(name) self.parent(name) end |
|---|
| 31 | |
|---|
| 32 | # デフォルトシンタックスの定義 |
|---|
| 33 | def self.default_syntax(syntax) |
|---|
| 34 | const_get(:DEFAULT_SYNTAX).replace(syntax) |
|---|
| 35 | end |
|---|
| 36 | def self.default_syntax=(syntax) self.default_syntax(syntax) end |
|---|
| 37 | |
|---|
| 38 | # デフォルトprocesの定義 |
|---|
| 39 | def self.process |
|---|
| 40 | define_method(:process_default) { |
|---|
| 41 | yield @text, @children, @lines |
|---|
| 42 | } |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 45 | # actionの定義 |
|---|
| 46 | def self.action |
|---|
| 47 | define_method(:action) { |
|---|
| 48 | yield @text, @context, @lines |
|---|
| 49 | } |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | # preprocessの定義 |
|---|
| 53 | def self.preprocess |
|---|
| 54 | define_method(:preprocess) { |
|---|
| 55 | yield @text, @children |
|---|
| 56 | } |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | # process_*の定義 |
|---|
| 60 | def self.method_missing(name, *args) |
|---|
| 61 | if block_given? && name.to_s =~ /process_([^=]+)/ |
|---|
| 62 | # process_*の定義 |
|---|
| 63 | define_method(name) { |
|---|
| 64 | yield @text, @children, @lines |
|---|
| 65 | } |
|---|
| 66 | return |
|---|
| 67 | end |
|---|
| 68 | raise NameError, "undefined method `#{name}' for #{self.inspect}:#{self.class}" |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | # デフォルトprocessのデフォルト実装 |
|---|
| 72 | def process_default |
|---|
| 73 | children.to_s |
|---|
| 74 | end |
|---|
| 75 | |
|---|
| 76 | # preprocessのデフォルト実装 |
|---|
| 77 | def preprocess |
|---|
| 78 | if self.class.action? |
|---|
| 79 | @text = text |
|---|
| 80 | else |
|---|
| 81 | @text = Parser.inline.bless(@text) |
|---|
| 82 | end |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | # multilineの終了マークの定義 |
|---|
| 86 | def self.multiline(end_mark) |
|---|
| 87 | const_get(:MULTILINE).replace(end_mark) |
|---|
| 88 | end |
|---|
| 89 | def self.multiline=(end_mark) self.multiline(end_mark) end |
|---|
| 90 | |
|---|
| 91 | # closerの定義 |
|---|
| 92 | def self.closer |
|---|
| 93 | const_get(:CLOSER).set |
|---|
| 94 | end |
|---|
| 95 | |
|---|
| 96 | # 構造化用メソッド |
|---|
| 97 | def initialize(parent, text, lines = []) |
|---|
| 98 | @text = text |
|---|
| 99 | @parent = parent |
|---|
| 100 | @children = Children.new |
|---|
| 101 | @lines = lines |
|---|
| 102 | preprocess |
|---|
| 103 | end |
|---|
| [2297] | 104 | attr_reader :parent, :text, :children, :lines |
|---|
| [2107] | 105 | |
|---|
| 106 | def add_child(child) |
|---|
| 107 | @children.push(child) |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | def self.containable?(element) |
|---|
| 111 | self::UNCONTAIN.each {|c| |
|---|
| 112 | return false if element <= c |
|---|
| 113 | } |
|---|
| 114 | self::CONTAIN.each {|c| |
|---|
| 115 | return true if element <= c |
|---|
| 116 | } |
|---|
| 117 | return false |
|---|
| 118 | end |
|---|
| 119 | |
|---|
| 120 | def self.action? |
|---|
| 121 | method_defined?(:action) |
|---|
| 122 | end |
|---|
| 123 | |
|---|
| 124 | def self.multiline? |
|---|
| 125 | self::MULTILINE.set? |
|---|
| 126 | end |
|---|
| 127 | |
|---|
| [2245] | 128 | #MULTILINE_END_MARK_PAIRS = [ |
|---|
| 129 | # '()[]{}<>', |
|---|
| 130 | # ')(][}{><' |
|---|
| 131 | #] |
|---|
| [2107] | 132 | def self.multiline_end_mark |
|---|
| 133 | # FIXME |
|---|
| 134 | #if self::MULTILINE.obj.nil? |
|---|
| 135 | # # multiline_end_mark自動生成 |
|---|
| 136 | # self::DEFAULT_SYNTAX |
|---|
| 137 | #else |
|---|
| 138 | self::MULTILINE.obj |
|---|
| 139 | #end |
|---|
| 140 | end |
|---|
| 141 | |
|---|
| 142 | def self.closer? |
|---|
| 143 | self::CLOSER.set? |
|---|
| 144 | end |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | # フォーマット変換用メソッド |
|---|
| 148 | def self.inline_parser=(parser) |
|---|
| 149 | @@inline = parser |
|---|
| 150 | end |
|---|
| 151 | |
|---|
| 152 | def self.format_type=(format_type) |
|---|
| 153 | @@format_type = format_type |
|---|
| 154 | @@process_method = "process_#{format_type}".to_sym |
|---|
| 155 | end |
|---|
| 156 | |
|---|
| 157 | def action_caller(context) |
|---|
| 158 | @context = context |
|---|
| 159 | action |
|---|
| 160 | @context = nil |
|---|
| 161 | end |
|---|
| 162 | |
|---|
| 163 | def process |
|---|
| 164 | if self.class.method_defined?(@@process_method) |
|---|
| 165 | __send__(@@process_method) |
|---|
| 166 | else |
|---|
| 167 | process_default |
|---|
| 168 | end |
|---|
| 169 | end |
|---|
| 170 | alias :to_s :process |
|---|
| 171 | |
|---|
| 172 | class Children < Array |
|---|
| 173 | def process |
|---|
| 174 | body = XMLRaw.new |
|---|
| 175 | each {|c| |
|---|
| 176 | body << c.process.to_s |
|---|
| 177 | } |
|---|
| 178 | body |
|---|
| 179 | end |
|---|
| 180 | alias :to_s :process |
|---|
| 181 | alias :to_str :to_s |
|---|
| 182 | end |
|---|
| 183 | |
|---|
| 184 | end |
|---|
| 185 | |
|---|
| 186 | |
|---|