Module: Lich::Common::Markup
- Defined in:
- documented/common/markup.rb
Overview
Frontend markup translation for the server stream.
Five concerns, all string-in/string-out:
* +fb_to_sf+ - normalizes a client line on the way upstream
* +sf_to_wiz+ - rewrites a StormFront/XML line into the GSL escape
vocabulary an old Wizard frontend understands
* +strip_xml+ - removes markup entirely, for scripts that want text
* +monsterbold_*+ - the emphasis pair for whatever frontend is attached
* +strip_game_links+ / +collapse_double_bold+ - give a front end that
predates them DragonRealms' legacy line shape
Lifted out of global_defs.rb, where it could not be unit-tested: that file cannot be required from a spec without redefining the whole script-facing DSL (respond, get, put...) against production game infrastructure. The six global wrappers remain in global_defs.rb and delegate here, because scripts in the wild call them unqualified.
State note: sf_to_wiz and strip_xml both carry unterminated markup across calls, because a pushStream element can be split over two reads. Those buffers stay in the process-globals they have always lived in ($sftowiz_multiline, $strip_xml_multiline) rather than becoming module state -- games.rb clears $strip_xml_multiline directly on reconnect (see games.rb:435), and that remains a supported reach-in.
Constant Summary collapse
- SUPPRESSED_STREAMS =
Streams whose content is presented elsewhere in the frontend (the spell window, the inventory pane) and so must not be echoed inline.
/<pushStream id=["'](?:spellfront|inv|bounty|society|reserve|speech|talk)["'][^>]*\/>.*?<popStream[^>]*>/m.freeze
- SPELLS_STREAM =
Regexp matching a Spells stream block from the server XML feed.
/<stream id="Spells">.*?<\/stream>/m.freeze
- DATA_ELEMENTS =
Paired elements carrying data for the status bars, not prose.
/<(compDef|inv|component|right|left|spell|prompt)[^>]*>.*?<\/\1>/m.freeze
- ANY_TAG =
Regexp matching any XML or HTML tag.
/<[^>]+>/.freeze
- LINK_TAG =
%r{<a(?:\s[^>]*)?>|</a>}.freeze
- GAME_LINK =
Regexp matching the opening tag of a game object link.
Matches tags with an exist attribute, which are links to in-game objects. Script-injected tags without exist are not matched.
/<a\s[^>]*\bexist=/.freeze
- DOUBLE_PUSH_BOLD =
'<pushBold/><pushBold/>'- DOUBLE_POP_BOLD =
'<popBold/><popBold/>'- LINK_BOLD =
A single bold pair around nothing but an optional article and one game link, not part of a doubled pair: the creature names in action and assess lines.
%r{(?<!<pushBold/>)<pushBold/>((?:(?:[Aa]n?|[Tt]he) )?<a\s[^>]*\bexist=[^>]*>[^<]*</a>)<popBold/>(?!<popBold/>)}.freeze
Class Method Summary collapse
-
.collapse_double_bold(line) ⇒ String
Collapses the doubled bold the game puts around a linked creature (
a <a ...>moradu ) to the single pair of DragonRealms' legacy line. -
.fb_to_sf(line) ⇒ String?
Normalize a line arriving from the client.
-
.monsterbold_end ⇒ String
Frontend-appropriate emphasis close.
-
.monsterbold_start ⇒ String
Frontend-appropriate emphasis open.
-
.sf_to_wiz(line, bypass_multiline: false) ⇒ String?
Rewrite an XML server line into the GSL escapes a Wizard-family frontend understands.
-
.strip_game_links(line) ⇒ String
Removes the game's object links, keeping their text, so a front end that predates them gets DragonRealms' legacy line:.
-
.strip_link_bold(line) ⇒ String
Removes the single bold the game puts around a linked creature outside room-object listings, so a front end that predates it gets DragonRealms' legacy line, where those names are plain:.
-
.strip_xml(line, type: nil) ⇒ String?
Strip game markup from a server-stream fragment.
Class Method Details
.collapse_double_bold(line) ⇒ String
Collapses the doubled bold the game puts around a linked creature
(
It runs after the downstream hooks and collapses pushes and pops
separately, so it trusts a hook that adds bold not to make a new
double: a hook
246 247 248 249 250 |
# File 'documented/common/markup.rb', line 246 def collapse_double_bold(line) return line unless line.include?(DOUBLE_PUSH_BOLD) || line.include?(DOUBLE_POP_BOLD) line.gsub(DOUBLE_PUSH_BOLD, '<pushBold/>').gsub(DOUBLE_POP_BOLD, '<popBold/>') end |
.fb_to_sf(line) ⇒ String?
Normalize a line arriving from the client.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'documented/common/markup.rb', line 84 def fb_to_sf(line) return line if line == "\r\n" line = line.gsub(/<c>/, "") return nil if line.gsub("\r\n", '').length < 1 line rescue StandardError report_error('fb_to_sf', line) nil end |
.monsterbold_end ⇒ String
Returns frontend-appropriate emphasis close.
165 166 167 168 169 170 |
# File 'documented/common/markup.rb', line 165 def monsterbold_end if ::Frontend.supports_gsl? then "\034GSM\r\n" elsif ::Frontend.supports_xml? then '<popBold/>' else '' end end |
.monsterbold_start ⇒ String
Returns frontend-appropriate emphasis open.
157 158 159 160 161 162 |
# File 'documented/common/markup.rb', line 157 def monsterbold_start if ::Frontend.supports_gsl? then "\034GSL\r\n" elsif ::Frontend.supports_xml? then '<pushBold/>' else '' end end |
.sf_to_wiz(line, bypass_multiline: false) ⇒ String?
Rewrite an XML server line into the GSL escapes a Wizard-family frontend understands.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'documented/common/markup.rb', line 105 def sf_to_wiz(line, bypass_multiline: false) return line if line == "\r\n" unless bypass_multiline line = buffer_sf_multiline(line) return nil if line.nil? end line = translate_launch_url(line) line = translate_speech(line) line = translate_thoughts(line) line = translate_voln(line) line = translate_familiar(line) line = translate_death(line) line = translate_room(line) line = translate_bold(line) line = line.gsub(SUPPRESSED_STREAMS, '') line = line.gsub(SPELLS_STREAM, '') line = line.gsub(DATA_ELEMENTS, '') line = line.gsub(ANY_TAG, '') line = line.gsub('>', '>').gsub('<', '<').gsub('&', '&') return nil if line.gsub("\r\n", '').length < 1 line rescue StandardError report_error('sf_to_wiz', line) nil end |
.strip_game_links(line) ⇒ String
Removes the game's object links, keeping their text, so a front end that predates them gets DragonRealms' legacy line:
Also here: Universalist <a exist="-10700003" noun="Corwin">Corwin</a>.
Also here: Universalist Corwin.
Only elements with an exist attribute are game links; any other (a script's from a downstream hook) and its stay. Each closes the innermost still open in the string. A with none open is kept; the game sends every link whole within one string (none of the 2 million links in a day of DR logs spans two).
186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'documented/common/markup.rb', line 186 def strip_game_links(line) return line unless line.include?('<a') && line.match?(GAME_LINK) open_links = [] line.gsub(LINK_TAG) do |tag| if tag == '</a>' open_links.pop ? '' : tag else game_link = tag.match?(GAME_LINK) open_links << game_link game_link ? '' : tag end end end |
.strip_link_bold(line) ⇒ String
Removes the single bold the game puts around a linked creature outside room-object listings, so a front end that predates it gets DragonRealms' legacy line, where those names are plain:
The <pushBold/><a exist="1" noun="moradu">jeol moradu</a><popBold/> moves into a position to parry.
The <a exist="1" noun="moradu">jeol moradu</a> moves into a position to parry.
<d cmd='look #1'><pushBold/>a <a exist="1" noun="moradu">jeol moradu</a><popBold/></d>
<d cmd='look #1'>a <a exist="1" noun="moradu">jeol moradu</a></d>
Only a pair around nothing but an optional article (a, an, the) and one game link goes. Room-object listings double their bold (collapse_double_bold keeps one pair there, as the legacy stream did), and a bold line or phrase with more text in it, such as a combat damage line, keeps its bold. Run it before strip_game_links, which removes the link it keys on.
It tells a listing from an action line only by that doubling, which
every captured listing has (0 single pairs in room objs). If the game
ever sends a listing with single bold, this strips it too, in all
three places a listing reaches it: the room objs component, the
227 228 229 230 231 |
# File 'documented/common/markup.rb', line 227 def strip_link_bold(line) return line unless line.include?('<pushBold/>') && line.match?(LINK_BOLD) line.gsub(LINK_BOLD, '\\1') end |