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.

Examples:

"<stream id=\"Spells\"><spell><name>Magic Missile</name></spell></stream>".match?(SPELLS_STREAM) #=> true

Returns:

  • (Regexp) —

    a pattern that captures the opening tag, all content between, and the closing tag

See Also:

/<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.

Examples:

"<pushBold/>".match?(ANY_TAG) #=> true
"</style>".match?(ANY_TAG) #=> true

Returns:

  • (Regexp) —

    a pattern that captures an opening or closing tag with any attributes and content

See Also:

/<[^>]+>/.freeze
%r{<a(?:\s[^>]*)?>|</a>}.freeze
/<a\s[^>]*\bexist=/.freeze
DOUBLE_PUSH_BOLD =
'<pushBold/><pushBold/>'
DOUBLE_POP_BOLD =
'<popBold/><popBold/>'
%r{(?<!<pushBold/>)<pushBold/>((?:(?:[Aa]n?|[Tt]he) )?<a\s[^>]*\bexist=[^>]*>[^<]*</a>)<popBold/>(?!<popBold/>)}.freeze

Class Method Summary collapse

Class Method Details

.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. Front ends that keep bold as a flag, or match creatures with one pushBold/popBold pair (Genie's monster count, Frostbite's bold creature list), need it.

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 right before a single game , with its elsewhere, leaves one push and two pops.

Parameters:

  • line (String) —

    one server string on its way to the client

Returns:

  • (String) —

    the line, or the same object when it has no doubled bold



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.

Parameters:

  • line (String) —

    one client line

Returns:

  • (String, nil) —

    the line with removed, or nil when nothing printable remains



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.

Returns:

  • (String) —

    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.

Returns:

  • (String) —

    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.

Parameters:

  • line (String) —

    one server line

  • bypass_multiline (Boolean) (defaults to: false) —

    skip the split-element buffer. Callers synthesizing a whole line of their own (messaging.rb) pass true so a stray unbalanced tag cannot swallow it.

Returns:

  • (String, nil) —

    nil while buffering a split element, or when nothing printable remains



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('&gt;', '>').gsub('&lt;', '<').gsub('&amp;', '&')
  return nil if line.gsub("\r\n", '').length < 1

  line
rescue StandardError
  report_error('sf_to_wiz', line)
  nil
end

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).

Parameters:

  • line (String) —

    one server string on its way to the client

Returns:

  • (String) —

    the line, or the same object when it has no link



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

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 block's , and the roomDesc line RoomBlock builds from it. The parser already accepts single bold there; this would then need to skip those spans by structure.

Parameters:

  • line (String) —

    one server string on its way to the client

Returns:

  • (String) —

    the line, or the same object when it has no such bold



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

.strip_xml(line, type: nil) ⇒ String?

Strip game markup from a server-stream fragment.

Parameters:

  • line (String) —

    one server-stream fragment

  • type (String, Symbol, nil) (defaults to: nil) —

    optional multiline buffer key. When nil the fragment is stripped statelessly. When given, unfinished pushStream content accumulates in a type-keyed buffer until a balancing popStream arrives, so an element split across reads is reassembled before stripping.

Returns:

  • (String, nil) —

    nil when nothing printable remains, or while a typed fragment is still being accumulated



145
146
147
# File 'documented/common/markup.rb', line 145

def strip_xml(line, type: nil)
  type.nil? ? strip_simple(line) : strip_multiline(line, type)
end