gamefic 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gamefic.rb +1 -2
- data/lib/gamefic/character.rb +31 -45
- data/lib/gamefic/director/delegate.rb +46 -24
- data/lib/gamefic/director/order.rb +7 -0
- data/lib/gamefic/director/parser.rb +11 -11
- data/lib/gamefic/engine/base.rb +2 -3
- data/lib/gamefic/entity.rb +8 -26
- data/lib/gamefic/plot.rb +38 -242
- data/lib/gamefic/plot/callbacks.rb +101 -0
- data/lib/gamefic/plot/command_mount.rb +70 -40
- data/lib/gamefic/plot/entities.rb +82 -0
- data/lib/gamefic/plot/host.rb +46 -0
- data/lib/gamefic/plot/playbook.rb +192 -0
- data/lib/gamefic/plot/players.rb +15 -0
- data/lib/gamefic/plot/scene_mount.rb +69 -31
- data/lib/gamefic/plot/snapshot.rb +20 -5
- data/lib/gamefic/scene/active.rb +8 -1
- data/lib/gamefic/scene/base.rb +4 -26
- data/lib/gamefic/scene/custom.rb +53 -3
- data/lib/gamefic/scene/multiple_choice.rb +1 -0
- data/lib/gamefic/scene/yes_or_no.rb +1 -1
- data/lib/gamefic/scene_data/multiple_scene.rb +0 -4
- data/lib/gamefic/shell.rb +0 -1
- data/lib/gamefic/source/file.rb +1 -1
- data/lib/gamefic/stage.rb +10 -2
- data/lib/gamefic/subplot.rb +70 -53
- data/lib/gamefic/syntax.rb +9 -2
- data/lib/gamefic/tester.rb +1 -1
- data/lib/gamefic/text.rb +8 -0
- data/lib/gamefic/{ansi.rb → text/ansi.rb} +12 -15
- data/lib/gamefic/text/html.rb +68 -0
- data/lib/gamefic/text/html/conversions.rb +250 -0
- data/lib/gamefic/text/html/entities.rb +9 -0
- data/lib/gamefic/tty.rb +2 -0
- data/lib/gamefic/user/tty.rb +2 -4
- data/lib/gamefic/version.rb +1 -1
- metadata +12 -8
- data/lib/gamefic/direction.rb +0 -46
- data/lib/gamefic/html.rb +0 -68
- data/lib/gamefic/html_to_ansi.rb +0 -185
- data/lib/gamefic/plot/entity_mount.rb +0 -45
- data/lib/gamefic/rule.rb +0 -18
@@ -0,0 +1,9 @@
|
|
1
|
+
module Gamefic
|
2
|
+
|
3
|
+
module Text
|
4
|
+
module Html
|
5
|
+
ENTITIES = { """ => "\"", "&" => "&", "<" => "<", ">" => ">", " " => " ", "¡" => "¡", "¢" => "¢", "£" => "£", "¤" => "¤", "¥" => "¥", "¦" => "¦", "§" => "§", "¨" => "¨", "©" => "©", "ª" => "ª", "«" => "«", "¬" => "¬", "­" => "", "®" => "®", "¯" => "¯", "°" => "°", "±" => "±", "²" => "²", "³" => "³", "´" => "´", "µ" => "µ", "¶" => "¶", "·" => "·", "¸" => "¸", "¹" => "¹", "º" => "º", "»" => "»", "¼" => "¼", "½" => "½", "¾" => "¾", "¿" => "¿", "À" => "À", "Á" => "Á", "Â" => "Â", "Ã" => "Ã", "Ä" => "Ä", "Å" => "Å", "Æ" => "Æ", "Ç" => "Ç", "È" => "È", "É" => "É", "Ê" => "Ê", "Ë" => "Ë", "Ì" => "Ì", "Í" => "Í", "Î" => "Î", "Ï" => "Ï", "Ð" => "Ð", "Ñ" => "Ñ", "Ò" => "Ò", "Ó" => "Ó", "Ô" => "Ô", "Õ" => "Õ", "Ö" => "Ö", "×" => "×", "Ø" => "Ø", "Ù" => "Ù", "Ú" => "Ú", "Û" => "Û", "Ü" => "Ü", "Ý" => "Ý", "Þ" => "Þ", "ß" => "ß", "à" => "à", "á" => "á", "â" => "â", "ã" => "ã", "ä" => "ä", "å" => "å", "æ" => "æ", "ç" => "ç", "è" => "è", "é" => "é", "ê" => "ê", "ë" => "ë", "ì" => "ì", "í" => "í", "î" => "î", "ï" => "ï", "ð" => "ð", "ñ" => "ñ", "ò" => "ò", "ó" => "ó", "ô" => "ô", "õ" => "õ", "ö" => "ö", "÷" => "÷", "ø" => "ø", "ù" => "ù", "ú" => "ú", "û" => "û", "ü" => "ü", "ý" => "ý", "þ" => "þ", "ÿ" => "ÿ", "Œ" => "Œ", "œ" => "œ", "Š" => "Š", "š" => "š", "Ÿ" => "Ÿ", "ƒ" => "ƒ", "ˆ" => "ˆ", "˜" => "˜", "Α" => "Α", "Β" => "Β", "Γ" => "Γ", "Δ" => "Δ", "Ε" => "Ε", "Ζ" => "Ζ", "Η" => "Η", "Θ" => "Θ", "Ι" => "Ι", "Κ" => "Κ", "Λ" => "Λ", "Μ" => "Μ", "Ν" => "Ν", "Ξ" => "Ξ", "Ο" => "Ο", "Π" => "Π", "Ρ" => "Ρ", "Σ" => "Σ", "Τ" => "Τ", "Υ" => "Υ", "Φ" => "Φ", "Χ" => "Χ", "Ψ" => "Ψ", "Ω" => "Ω", "α" => "α", "β" => "β", "γ" => "γ", "δ" => "δ", "ε" => "ε", "ζ" => "ζ", "η" => "η", "θ" => "θ", "ι" => "ι", "κ" => "κ", "λ" => "λ", "μ" => "μ", "ν" => "ν", "ξ" => "ξ", "ο" => "ο", "π" => "π", "ρ" => "ρ", "ς" => "ς", "σ" => "σ", "τ" => "τ", "υ" => "υ", "φ" => "φ", "χ" => "χ", "ψ" => "ψ", "ω" => "ω", "ϑ" => "ϑ", "ϒ" => "ϒ", "ϖ" => "ϖ", " " => " ", " " => " ", " " => " ", "‌" => "", "‍" => "", "‎" => "", "‏" => "", "–" => "–", "—" => "—", "‘" => "‘", "’" => "’", "‚" => "‚", "“" => "“", "”" => "”", "„" => "„", "†" => "†", "‡" => "‡", "•" => "•", "…" => "…", "‰" => "‰", "′" => "′", "″" => "″", "‹" => "‹", "›" => "›", "‾" => "‾", "⁄" => "⁄", "€" => "€", "ℑ" => "ℑ", "℘" => "℘", "ℜ" => "ℜ", "™" => "™", "ℵ" => "ℵ", "←" => "←", "↑" => "↑", "→" => "→", "↓" => "↓", "↔" => "↔", "↵" => "↵", "⇐" => "⇐", "⇑" => "⇑", "⇒" => "⇒", "⇓" => "⇓", "⇔" => "⇔", "∀" => "∀", "∂" => "∂", "∃" => "∃", "∅" => "∅", "∇" => "∇", "∈" => "∈", "∉" => "∉", "∋" => "∋", "∏" => "∏", "∑" => "∑", "−" => "−", "∗" => "∗", "√" => "√", "∝" => "∝", "∞" => "∞", "∠" => "∠", "∧" => "∧", "∨" => "∨", "∩" => "∩", "∪" => "∪", "∫" => "∫", "∴" => "∴", "∼" => "∼", "≅" => "≅", "≈" => "≈", "≠" => "≠", "≡" => "≡", "≤" => "≤", "≥" => "≥", "⊂" => "⊂", "⊃" => "⊃", "⊄" => "⊄", "⊆" => "⊆", "⊇" => "⊇", "⊕" => "⊕", "⊗" => "⊗", "⊥" => "⊥", "⋅" => "⋅", "⌈" => "⌈", "⌉" => "⌉", "⌊" => "⌊", "⌋" => "⌋", "⟨" => "〈", "⟩" => "〉", "◊" => "◊", "♠" => "♠", "♣" => "♣", "♥" => "♥", "♦" => "♦" }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
data/lib/gamefic/tty.rb
CHANGED
data/lib/gamefic/user/tty.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'gamefic/engine'
|
2
2
|
require 'json'
|
3
|
-
require 'gamefic/
|
3
|
+
require 'gamefic/text'
|
4
4
|
|
5
5
|
module Gamefic
|
6
6
|
|
7
7
|
# Extend User::Base to convert HTML into ANSI text.
|
8
8
|
#
|
9
9
|
class User::Tty < User::Base
|
10
|
-
include HtmlToAnsi
|
11
|
-
|
12
10
|
def save filename, snapshot
|
13
11
|
data = snapshot.merge(:metadata => @character.plot.metadata)
|
14
12
|
json = JSON.generate data
|
@@ -48,7 +46,7 @@ module Gamefic
|
|
48
46
|
end
|
49
47
|
|
50
48
|
def flush
|
51
|
-
html_to_ansi(super)
|
49
|
+
Gamefic::Text::Html::Conversions.html_to_ansi(super)
|
52
50
|
end
|
53
51
|
end
|
54
52
|
|
data/lib/gamefic/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gamefic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fred Snyder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -134,13 +134,11 @@ files:
|
|
134
134
|
- bin/gamefic
|
135
135
|
- lib/gamefic.rb
|
136
136
|
- lib/gamefic/action.rb
|
137
|
-
- lib/gamefic/ansi.rb
|
138
137
|
- lib/gamefic/character.rb
|
139
138
|
- lib/gamefic/command.rb
|
140
139
|
- lib/gamefic/core_ext/array.rb
|
141
140
|
- lib/gamefic/core_ext/string.rb
|
142
141
|
- lib/gamefic/describable.rb
|
143
|
-
- lib/gamefic/direction.rb
|
144
142
|
- lib/gamefic/director.rb
|
145
143
|
- lib/gamefic/director/delegate.rb
|
146
144
|
- lib/gamefic/director/order.rb
|
@@ -159,14 +157,16 @@ files:
|
|
159
157
|
- lib/gamefic/grammar/verb_set.rb
|
160
158
|
- lib/gamefic/grammar/verbs.rb
|
161
159
|
- lib/gamefic/grammar/word_adapter.rb
|
162
|
-
- lib/gamefic/html.rb
|
163
|
-
- lib/gamefic/html_to_ansi.rb
|
164
160
|
- lib/gamefic/keywords.rb
|
165
161
|
- lib/gamefic/node.rb
|
166
162
|
- lib/gamefic/plot.rb
|
167
163
|
- lib/gamefic/plot/article_mount.rb
|
164
|
+
- lib/gamefic/plot/callbacks.rb
|
168
165
|
- lib/gamefic/plot/command_mount.rb
|
169
|
-
- lib/gamefic/plot/
|
166
|
+
- lib/gamefic/plot/entities.rb
|
167
|
+
- lib/gamefic/plot/host.rb
|
168
|
+
- lib/gamefic/plot/playbook.rb
|
169
|
+
- lib/gamefic/plot/players.rb
|
170
170
|
- lib/gamefic/plot/query_mount.rb
|
171
171
|
- lib/gamefic/plot/scene_mount.rb
|
172
172
|
- lib/gamefic/plot/snapshot.rb
|
@@ -184,7 +184,6 @@ files:
|
|
184
184
|
- lib/gamefic/query/self.rb
|
185
185
|
- lib/gamefic/query/siblings.rb
|
186
186
|
- lib/gamefic/query/text.rb
|
187
|
-
- lib/gamefic/rule.rb
|
188
187
|
- lib/gamefic/scene.rb
|
189
188
|
- lib/gamefic/scene/active.rb
|
190
189
|
- lib/gamefic/scene/base.rb
|
@@ -213,6 +212,11 @@ files:
|
|
213
212
|
- lib/gamefic/subplot.rb
|
214
213
|
- lib/gamefic/syntax.rb
|
215
214
|
- lib/gamefic/tester.rb
|
215
|
+
- lib/gamefic/text.rb
|
216
|
+
- lib/gamefic/text/ansi.rb
|
217
|
+
- lib/gamefic/text/html.rb
|
218
|
+
- lib/gamefic/text/html/conversions.rb
|
219
|
+
- lib/gamefic/text/html/entities.rb
|
216
220
|
- lib/gamefic/tty.rb
|
217
221
|
- lib/gamefic/user.rb
|
218
222
|
- lib/gamefic/user/base.rb
|
data/lib/gamefic/direction.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Gamefic
|
2
|
-
|
3
|
-
class Direction
|
4
|
-
attr_accessor :name, :adjective, :adverb, :reverse
|
5
|
-
def initialize args = {}
|
6
|
-
args.each { |key, value|
|
7
|
-
send "#{key}=", value
|
8
|
-
}
|
9
|
-
if !reverse.nil?
|
10
|
-
reverse.reverse = self
|
11
|
-
end
|
12
|
-
proper_named = true
|
13
|
-
end
|
14
|
-
def adjective
|
15
|
-
@adjective || @name
|
16
|
-
end
|
17
|
-
def adverb
|
18
|
-
@adverb || @name
|
19
|
-
end
|
20
|
-
def reverse=(dir)
|
21
|
-
@reverse = dir
|
22
|
-
end
|
23
|
-
def synonyms
|
24
|
-
"#{adjective} #{adverb}"
|
25
|
-
end
|
26
|
-
def to_s
|
27
|
-
@name
|
28
|
-
end
|
29
|
-
def self.find(str)
|
30
|
-
x = { "north" => NORTH, "south" => SOUTH, "west" => WEST, "east" => EAST, "up" => UP, "down" => DOWN, "northwest" => NORTHWEST, "northeast" => NORTHEAST, "southwest" => SOUTHWEST, "southeast" => SOUTHEAST }
|
31
|
-
x[str]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
NORTH = Direction.new(:name => 'north', :adjective => 'northern')
|
36
|
-
SOUTH = Direction.new(:name => 'south', :adjective => 'southern', :reverse => NORTH)
|
37
|
-
WEST = Direction.new(:name => 'west', :adjective => 'western')
|
38
|
-
EAST = Direction.new(:name => 'east', :adjective => 'eastern', :reverse => WEST)
|
39
|
-
NORTHWEST = Direction.new(:name => 'northwest', :adjective => 'northwestern')
|
40
|
-
SOUTHEAST = Direction.new(:name => 'southeast', :adjective => 'southeastern', :reverse => NORTHWEST)
|
41
|
-
NORTHEAST = Direction.new(:name => 'northeast', :adjective => 'northeastern')
|
42
|
-
SOUTHWEST = Direction.new(:name => 'southwest', :adjective => 'southwestern', :reverse => NORTHEAST)
|
43
|
-
UP = Direction.new(:name => 'up', :adjective => 'upwards')
|
44
|
-
DOWN = Direction.new(:name => 'down', :adjective => 'downwards', :reverse => UP)
|
45
|
-
|
46
|
-
end
|
data/lib/gamefic/html.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'rexml/document'
|
2
|
-
|
3
|
-
module Gamefic
|
4
|
-
|
5
|
-
module Html
|
6
|
-
# Convert ampersands to &
|
7
|
-
#
|
8
|
-
# @param text [String]
|
9
|
-
# @return [String]
|
10
|
-
def self.fix_ampersands(text)
|
11
|
-
codes = []
|
12
|
-
ENTITIES.keys.each { |e|
|
13
|
-
codes.push e[1..-1]
|
14
|
-
}
|
15
|
-
piped = codes.join('|')
|
16
|
-
re = Regexp.new("&(?!(#{piped}))")
|
17
|
-
text.gsub(re, '&\1')
|
18
|
-
end
|
19
|
-
|
20
|
-
# Encode a String with HTML entities
|
21
|
-
#
|
22
|
-
# @param text [String]
|
23
|
-
# @return [String]
|
24
|
-
def self.encode(text)
|
25
|
-
encoded = text
|
26
|
-
Gamefic::Html::ENTITIES.each { |k, v|
|
27
|
-
encoded = encoded.gsub(v, k)
|
28
|
-
}
|
29
|
-
encoded
|
30
|
-
end
|
31
|
-
|
32
|
-
# Decode a String's HTML entities
|
33
|
-
#
|
34
|
-
# @param text [String]
|
35
|
-
# @return [String]
|
36
|
-
def self.decode(text)
|
37
|
-
Gamefic::Html::ENTITIES.each { |k, v|
|
38
|
-
text = text.gsub(k, v)
|
39
|
-
}
|
40
|
-
text
|
41
|
-
end
|
42
|
-
|
43
|
-
# Parse a String into an XML document
|
44
|
-
#
|
45
|
-
# @param code [String]
|
46
|
-
# @return [REXML::Document]
|
47
|
-
def self.parse(code)
|
48
|
-
code = fix_ampersands(code).strip
|
49
|
-
last = nil
|
50
|
-
begin
|
51
|
-
doc = REXML::Document.new code
|
52
|
-
rescue REXML::ParseException => e
|
53
|
-
# Convert invalid < characters to <
|
54
|
-
if e.source.buffer != last and e.source.buffer[0,1] == '<'
|
55
|
-
code = code[0,(code.length - e.source.buffer.length)] + '<' + e.source.buffer[1..-1]
|
56
|
-
last = e.source.buffer
|
57
|
-
retry
|
58
|
-
end
|
59
|
-
raise e
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
module Html
|
65
|
-
ENTITIES = { """ => "\"", "&" => "&", "<" => "<", ">" => ">", " " => " ", "¡" => "¡", "¢" => "¢", "£" => "£", "¤" => "¤", "¥" => "¥", "¦" => "¦", "§" => "§", "¨" => "¨", "©" => "©", "ª" => "ª", "«" => "«", "¬" => "¬", "­" => "", "®" => "®", "¯" => "¯", "°" => "°", "±" => "±", "²" => "²", "³" => "³", "´" => "´", "µ" => "µ", "¶" => "¶", "·" => "·", "¸" => "¸", "¹" => "¹", "º" => "º", "»" => "»", "¼" => "¼", "½" => "½", "¾" => "¾", "¿" => "¿", "À" => "À", "Á" => "Á", "Â" => "Â", "Ã" => "Ã", "Ä" => "Ä", "Å" => "Å", "Æ" => "Æ", "Ç" => "Ç", "È" => "È", "É" => "É", "Ê" => "Ê", "Ë" => "Ë", "Ì" => "Ì", "Í" => "Í", "Î" => "Î", "Ï" => "Ï", "Ð" => "Ð", "Ñ" => "Ñ", "Ò" => "Ò", "Ó" => "Ó", "Ô" => "Ô", "Õ" => "Õ", "Ö" => "Ö", "×" => "×", "Ø" => "Ø", "Ù" => "Ù", "Ú" => "Ú", "Û" => "Û", "Ü" => "Ü", "Ý" => "Ý", "Þ" => "Þ", "ß" => "ß", "à" => "à", "á" => "á", "â" => "â", "ã" => "ã", "ä" => "ä", "å" => "å", "æ" => "æ", "ç" => "ç", "è" => "è", "é" => "é", "ê" => "ê", "ë" => "ë", "ì" => "ì", "í" => "í", "î" => "î", "ï" => "ï", "ð" => "ð", "ñ" => "ñ", "ò" => "ò", "ó" => "ó", "ô" => "ô", "õ" => "õ", "ö" => "ö", "÷" => "÷", "ø" => "ø", "ù" => "ù", "ú" => "ú", "û" => "û", "ü" => "ü", "ý" => "ý", "þ" => "þ", "ÿ" => "ÿ", "Œ" => "Œ", "œ" => "œ", "Š" => "Š", "š" => "š", "Ÿ" => "Ÿ", "ƒ" => "ƒ", "ˆ" => "ˆ", "˜" => "˜", "Α" => "Α", "Β" => "Β", "Γ" => "Γ", "Δ" => "Δ", "Ε" => "Ε", "Ζ" => "Ζ", "Η" => "Η", "Θ" => "Θ", "Ι" => "Ι", "Κ" => "Κ", "Λ" => "Λ", "Μ" => "Μ", "Ν" => "Ν", "Ξ" => "Ξ", "Ο" => "Ο", "Π" => "Π", "Ρ" => "Ρ", "Σ" => "Σ", "Τ" => "Τ", "Υ" => "Υ", "Φ" => "Φ", "Χ" => "Χ", "Ψ" => "Ψ", "Ω" => "Ω", "α" => "α", "β" => "β", "γ" => "γ", "δ" => "δ", "ε" => "ε", "ζ" => "ζ", "η" => "η", "θ" => "θ", "ι" => "ι", "κ" => "κ", "λ" => "λ", "μ" => "μ", "ν" => "ν", "ξ" => "ξ", "ο" => "ο", "π" => "π", "ρ" => "ρ", "ς" => "ς", "σ" => "σ", "τ" => "τ", "υ" => "υ", "φ" => "φ", "χ" => "χ", "ψ" => "ψ", "ω" => "ω", "ϑ" => "ϑ", "ϒ" => "ϒ", "ϖ" => "ϖ", " " => " ", " " => " ", " " => " ", "‌" => "", "‍" => "", "‎" => "", "‏" => "", "–" => "–", "—" => "—", "‘" => "‘", "’" => "’", "‚" => "‚", "“" => "“", "”" => "”", "„" => "„", "†" => "†", "‡" => "‡", "•" => "•", "…" => "…", "‰" => "‰", "′" => "′", "″" => "″", "‹" => "‹", "›" => "›", "‾" => "‾", "⁄" => "⁄", "€" => "€", "ℑ" => "ℑ", "℘" => "℘", "ℜ" => "ℜ", "™" => "™", "ℵ" => "ℵ", "←" => "←", "↑" => "↑", "→" => "→", "↓" => "↓", "↔" => "↔", "↵" => "↵", "⇐" => "⇐", "⇑" => "⇑", "⇒" => "⇒", "⇓" => "⇓", "⇔" => "⇔", "∀" => "∀", "∂" => "∂", "∃" => "∃", "∅" => "∅", "∇" => "∇", "∈" => "∈", "∉" => "∉", "∋" => "∋", "∏" => "∏", "∑" => "∑", "−" => "−", "∗" => "∗", "√" => "√", "∝" => "∝", "∞" => "∞", "∠" => "∠", "∧" => "∧", "∨" => "∨", "∩" => "∩", "∪" => "∪", "∫" => "∫", "∴" => "∴", "∼" => "∼", "≅" => "≅", "≈" => "≈", "≠" => "≠", "≡" => "≡", "≤" => "≤", "≥" => "≥", "⊂" => "⊂", "⊃" => "⊃", "⊄" => "⊄", "⊆" => "⊆", "⊇" => "⊇", "⊕" => "⊕", "⊗" => "⊗", "⊥" => "⊥", "⋅" => "⋅", "⌈" => "⌈", "⌉" => "⌉", "⌊" => "⌊", "⌋" => "⌋", "⟨" => "〈", "⟩" => "〉", "◊" => "◊", "♠" => "♠", "♣" => "♣", "♥" => "♥", "♦" => "♦" }
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
data/lib/gamefic/html_to_ansi.rb
DELETED
@@ -1,185 +0,0 @@
|
|
1
|
-
require 'gamefic/ansi'
|
2
|
-
require 'gamefic/html'
|
3
|
-
require 'io/console'
|
4
|
-
|
5
|
-
module Gamefic
|
6
|
-
|
7
|
-
module HtmlToAnsi
|
8
|
-
include Ansi
|
9
|
-
include Ansi::Code
|
10
|
-
|
11
|
-
def html_to_ansi text, wrap: true, width: nil
|
12
|
-
return '' if text.strip == ''
|
13
|
-
output = ''
|
14
|
-
begin
|
15
|
-
doc = Html.parse("<body>#{text.strip}</body>")
|
16
|
-
format_recursively doc
|
17
|
-
texts = REXML::XPath.match(doc, './/text()')
|
18
|
-
output = texts.join('').gsub(/'/, "'").gsub(/"/, '"').gsub(/</, '<').gsub(/>/, '>')
|
19
|
-
output += Ansi.graphics_mode(Attribute::NORMAL)
|
20
|
-
output = Html.decode(output)
|
21
|
-
rescue REXML::ParseException => e
|
22
|
-
output = Html.encode(text) + "\n\n"
|
23
|
-
#output = data
|
24
|
-
end
|
25
|
-
output.gsub!(/(\n\n)+/, "\n\n")
|
26
|
-
calc_width = size[0] || width
|
27
|
-
if calc_width.nil? or !wrap
|
28
|
-
output
|
29
|
-
else
|
30
|
-
terminalize(output, calc_width - 1)
|
31
|
-
end
|
32
|
-
output
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def format_recursively(top, stack = nil)
|
38
|
-
ol_index = 1
|
39
|
-
stack ||= [Attribute::NORMAL]
|
40
|
-
top.elements.each { |element|
|
41
|
-
formats = [Attribute::NORMAL]
|
42
|
-
classes = element.attribute('class').to_s.split(' ')
|
43
|
-
if classes.include?('hint')
|
44
|
-
formats.push Foreground::YELLOW
|
45
|
-
end
|
46
|
-
case element.name
|
47
|
-
when 'strong', 'b'
|
48
|
-
formats.push Attribute::BOLD
|
49
|
-
when 'em', 'i', 'u'
|
50
|
-
formats.push Attribute::UNDERSCORE
|
51
|
-
when 'a'
|
52
|
-
if element.attributes['rel'].to_s == 'gamefic'
|
53
|
-
element.attributes['href'] = element.attributes['href'][5..-1]
|
54
|
-
formats.push [Attribute::UNDERSCORE, Extra::COMMAND]
|
55
|
-
else
|
56
|
-
formats.push [Attribute::UNDERSCORE, Extra::HREF]
|
57
|
-
end
|
58
|
-
when 'li'
|
59
|
-
if top.name == 'ol'
|
60
|
-
element.text = "#{ol_index}. #{element.text}"
|
61
|
-
ol_index += 1
|
62
|
-
else
|
63
|
-
element.text = "* #{element.text}"
|
64
|
-
end
|
65
|
-
formats.push [Extra::LINE]
|
66
|
-
when 'img'
|
67
|
-
formats.push [Extra::IGNORED]
|
68
|
-
when 'body', 'p', 'ol', 'ul'
|
69
|
-
formats.push Extra::BLOCK
|
70
|
-
when 'pre'
|
71
|
-
formats.push [Extra::BLOCK, Extra::PRE]
|
72
|
-
when 'nav'
|
73
|
-
formats.push Extra::BLOCK
|
74
|
-
when 'h1', 'h2', 'h3', 'h4', 'h5'
|
75
|
-
formats.push [Attribute::BOLD, Extra::BLOCK, Extra::UPPERCASE]
|
76
|
-
when 'kbd'
|
77
|
-
formats.push [Foreground::GREEN]
|
78
|
-
end
|
79
|
-
if has_code?(stack, Extra::IGNORED)
|
80
|
-
element.parent.delete_element(element)
|
81
|
-
end
|
82
|
-
stack.push formats
|
83
|
-
if has_code?(stack, Extra::UPPERCASE)
|
84
|
-
element.texts.each { |text|
|
85
|
-
text.value.upcase!
|
86
|
-
}
|
87
|
-
end
|
88
|
-
element.texts.each { |text|
|
89
|
-
text.value = "#{Ansi.graphics_mode(*stack)}#{text.value}"
|
90
|
-
}
|
91
|
-
if has_code?(stack.last, Extra::IMAGE)
|
92
|
-
element.text = "#{element.attribute('alt') ? element.attribute('alt') : '[Image]'}"
|
93
|
-
end
|
94
|
-
format_recursively element, stack
|
95
|
-
if has_code?(stack.last, Extra::COMMAND)
|
96
|
-
if element.attribute('data-command').to_s != ''
|
97
|
-
tmp = stack.pop
|
98
|
-
element.add_text "#{Ansi.graphics_mode(*stack)}"
|
99
|
-
element.add_text " [#{element.attribute('data-command')}]"
|
100
|
-
stack.push tmp
|
101
|
-
element.add_text "#{Ansi.graphics_mode(*stack)}"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
if has_code?(stack.last, Extra::BLOCK) and !has_code?(stack.last, Extra::PRE)
|
105
|
-
element.texts.first.value.lstrip! unless element.texts.first.nil?
|
106
|
-
element.texts.last.value.rstrip! unless element.texts.last.nil?
|
107
|
-
element.texts.each { |t|
|
108
|
-
t.value = t.value.gsub(/ +/, ' ').strip
|
109
|
-
}
|
110
|
-
end
|
111
|
-
if has_code?(stack.last, Extra::BLOCK)
|
112
|
-
element.add_text("\n\n")
|
113
|
-
elsif has_code?(stack.last, Extra::LINE)
|
114
|
-
if !has_code?(stack[-2], Extra::BLOCK) || element != top.elements.to_a.last
|
115
|
-
element.add_text("\n")
|
116
|
-
end
|
117
|
-
end
|
118
|
-
if has_code?(stack.last, Extra::HREF)
|
119
|
-
if element.attribute('href').to_s != "#"
|
120
|
-
tmp = stack.pop
|
121
|
-
element.add_text "#{Ansi.graphics_mode(*stack)}"
|
122
|
-
element.add_text(" [#{element.attribute('href')}]")
|
123
|
-
stack.push tmp
|
124
|
-
element.add_text "#{Ansi.graphics_mode(*stack)}"
|
125
|
-
end
|
126
|
-
end
|
127
|
-
if has_code?(stack.last, Extra::IMAGE)
|
128
|
-
element.add_text(" [#{element.attribute('src')}]")
|
129
|
-
if !has_code?(stack, Extra::BLOCK)
|
130
|
-
element.add_text("\n\n")
|
131
|
-
end
|
132
|
-
end
|
133
|
-
stack.pop
|
134
|
-
}
|
135
|
-
end
|
136
|
-
|
137
|
-
def terminalize string, max_length
|
138
|
-
i = 0
|
139
|
-
output = ''
|
140
|
-
line_length = 0
|
141
|
-
while i < string.length
|
142
|
-
line_length += 1
|
143
|
-
char = string[i,1]
|
144
|
-
if char == "\e"
|
145
|
-
# Right now, graphics modes are the only supported ANSI sequences.
|
146
|
-
end_of_seq = string.index("m", i)
|
147
|
-
output += string[i..end_of_seq]
|
148
|
-
i = end_of_seq + 1
|
149
|
-
elsif char == " "
|
150
|
-
next_space = string.index(/[\s]/, i + 1)
|
151
|
-
if !next_space.nil? and line_length + (next_space - i) > max_length
|
152
|
-
output += "\n"
|
153
|
-
line_length = 0
|
154
|
-
else
|
155
|
-
output += char
|
156
|
-
end
|
157
|
-
i += 1
|
158
|
-
else
|
159
|
-
if char == "\n"
|
160
|
-
line_length = 0
|
161
|
-
end
|
162
|
-
output += char
|
163
|
-
i += 1
|
164
|
-
end
|
165
|
-
end
|
166
|
-
output #output.strip
|
167
|
-
end
|
168
|
-
|
169
|
-
def has_code?(fmt, code)
|
170
|
-
if fmt.kind_of?(Array)
|
171
|
-
return fmt.flatten.include?(code)
|
172
|
-
end
|
173
|
-
return fmt == code
|
174
|
-
end
|
175
|
-
|
176
|
-
def size
|
177
|
-
begin
|
178
|
-
return STDOUT.winsize.reverse
|
179
|
-
rescue
|
180
|
-
return [nil,nil]
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module Gamefic
|
2
|
-
|
3
|
-
module Plot::EntityMount
|
4
|
-
# Make a new Entity with the provided properties.
|
5
|
-
#
|
6
|
-
# @example Create an Entity
|
7
|
-
# chair = make Entity, :name => 'red chair'
|
8
|
-
# chair.name #=> 'red chair'
|
9
|
-
#
|
10
|
-
# @param cls [Class] The Class of the Entity to be created.
|
11
|
-
# @param args [Hash] The entity's properties.
|
12
|
-
# @return The Entity instance.
|
13
|
-
def make(cls, args = {}, &block)
|
14
|
-
ent = cls.new(self, args, &block)
|
15
|
-
if ent.kind_of?(Entity) == false
|
16
|
-
raise "Invalid entity class"
|
17
|
-
end
|
18
|
-
ent
|
19
|
-
end
|
20
|
-
# Pick an entity based on its description.
|
21
|
-
# The description provided must match exactly one entity; otherwise
|
22
|
-
# an error is raised.
|
23
|
-
#
|
24
|
-
# @example Select the Entity that matches the description
|
25
|
-
# red_chair = make Entity, :name => 'red chair'
|
26
|
-
# blue_chair make Entity, :name => 'blue chair'
|
27
|
-
# pick "red chair" #=> red_chair
|
28
|
-
# pick "blue chair" #=> blue_chair
|
29
|
-
# pick "chair" #=> IndexError: description is ambiguous
|
30
|
-
#
|
31
|
-
# @param @description [String] The description of the entity
|
32
|
-
# @return [Entity] The entity that matches the description
|
33
|
-
def pick(description)
|
34
|
-
query = Gamefic::Query::Base.new
|
35
|
-
result = query.match(description, entities)
|
36
|
-
if result.objects.length == 0
|
37
|
-
raise IndexError.new("Unable to find entity from '#{description}'")
|
38
|
-
elsif result.objects.length > 1
|
39
|
-
raise IndexError.new("Ambiguous entities found from '#{description}'")
|
40
|
-
end
|
41
|
-
result.objects[0]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|