Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
//substituteHTMLTags.js
/*global adventurejs A*/
"use strict";

/**
 * <strong>substituteHTMLTags</strong> acts on strings prior to
 * printing them to {@link adventurejs.Display|Display}.
 * Substitution is the last step of
 * {@link adventurejs.Game#print|Game.print()}.
 * It replaces text in &lt;angle&gt;brackets&lt;/&gt;
 * with &lt;span class="angle"&gt;brackets&lt;/span&gt;,
 * providing a shortcut method to adding CSS styles to text.
 * <br><br>
 * For example:
 * <br><br>
 *
 * MyGame.createAsset({
 *   class: "Room",
 *   name: "Standing Room",
 *   descriptions: {
 *     brief: "The north door is <hilite>open</>. ",
 *     through: "Through the door you see a <hilite>bright light</>. ",  *
 *     verbose: return function(){ `The north door
 *     ${MyGame.world.aurora_door.is.closed ?
 *     "is <hilite>closed</>, hiding the aurora. " :
 *     "is <hilite>open</>, revealing the $(northcolor) aurora light" }` }
 *   }
 * })
 * @memberOf adventurejs.Game
 * @method adventurejs.Game#substituteHTMLTags
 * @param {String} msg A string on which to perform substitutions.
 * @returns {String}
 */

adventurejs.substituteHTMLTags = function Adventurejs_substituteHTMLTags(msg) {
  //console.warn(msg);
  msg = msg.replace(
    /<([a-zA-Z\s_][a-zA-Z\s_\d]*)>([^<>]+?)<\/>/g,
    function (match, tagName, tagContent) {
      return `<span class="${tagName}">${tagContent}</span>`;
    }
  );
  return msg;
};