Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Display macro in abbr titles?

A very elegant way to do something I'm attempting with my story would be a working equivalent of the following:
<abbr title="<<display StatsPassage>>">Stats</abbr>

There probably isn't a way to make this work without some heavy coding, but I thought I'd try. If it's a huge deal then don't sweat it, I can figure something else out.

Comments

  • You cannot do exactly what you are trying, no. There may be a way to achieve the same result, however.

    What story format are you using and does the passage StatsPassage contain anything other than plain text?
  • edited September 2015
    Oops, I totally meant to mention I was using Sugarcube, and yeah unfortunately any passage I'd want to use for this purpose would include some heavy conditionals and stuff, so I can't just use the
    <<print '<abbr title=" + $var + "></abbr>'>>
    
    trick that, unless I'm way off base, you're going to suggest.
  • I meant more the result of the passage. You cannot simply cram any odd thing into an HTML tag's title attribute (at least, not without encoding it first).

    As for your guess, that's not exactly what my first idea was, though it's along those lines. However, since you mention using a story variable, you should easily be able to build a string within StatsPassage and do exactly that (well, not exactly that, your example has syntax errors).

    As another idea, would it be problematic to construct the <abbr> within StatsPassage and just display it to render the proper <abbr>? For example, within StatsPassage:
    <<if     …>><abbr title=" stuff ">Stats</abbr>\
    <<elseif …>><abbr title=" other stuff ">Stats</abbr>\
    <<elseif …>><abbr title=" yet other stuff  ">Stats</abbr>\
    <</if>>
    
    And then just display StatsPassage to yield the <abbr>:
    <<display "StatsPassage">>
    
    I don't know if you're using StatsPassage in other areas which would make this unsuitable.
  • edited September 2015
    What would be the proper syntax for the print macro trick? I've seen it around and tried using it, and I've had moderate success with a few other things. I might have to settle for it.

    Other than that, I suppose I can just be more specific about what I'm doing. I used "StatsPassage" as an example but the real passage name in the story is "Appearance" and what I'd like to be able to do is just have the word "Appearance" available to hover over for a popup description of what the PC looks like, which is something that can change over the events of the game.

    So the Appearance passage actually doesn't have any conditionals or anything (though if I can get the idea working I can apply it to other things that do use them), and looks something like:
    You are a $race $gender with $hairlength $haircolor hair and $eyecolor eyes.
    

    ...etc etc.

    Once the string variables are sorted out it becomes just text.
  • Charisma wrote: »
    What would be the proper syntax for the print macro trick? I've seen it around and tried using it, and I've had moderate success with a few other things. I might have to settle for it.
    Based on your example:
    <<print '<abbr title="' + $var + '"></abbr>'>>
    
    Your issue was that you omitted the first closing and second opening single quotes.

    As an example of building a string:
    <<print '<abbr title="' + escapeSpecial("You are a " + $race + " " + $gender + " with " + $hairlength + " " + $haircolor + " hair and " + $eyecolor + " eyes.") + '">Stats</abbr>'>>
    
    Which requires the encodeSpecial() function: (put it in a script tagged passage)
    window.encodeSpecial = function (text) {
    	var specials = {
    		"&" : "&amp;",
    		"<" : "&lt;",
    		">" : "&gt;",
    		'"' : "&quot;",
    		"'" : "&#039;"
    	};
    	return text.replace(/[&<>"']/g, function (c) { return specials[c]; });
    };
    

    Charisma wrote: »
    So the Appearance passage actually doesn't have any conditionals or anything (though if I can get the idea working I can apply it to other things that do use them), and looks something like:
    You are a $race $gender with $hairlength $haircolor hair and $eyecolor eyes.
    
    ...etc etc.

    Once the string variables are sorted out it becomes just text.
    Unfortunately, SugarCube's wikifier engine doesn't generate intermediary strings which can be grabbed. It transforms markup directly into object nodes, usually to eventually be inserted into the DOM.

    So, if you want some kind of generic solution that uses the wikifier, then you pretty much only have one approach available. Wikify the passage into a buffer element and then grab the innerHTML. For example:
    /*
    	The `tale.get("Appearance").processText()` bit returns the, mostly, raw text of
    	the "Appearance" passage.
    */
    <<print '<abbr title="' + wikifyToString(tale.get("Appearance").processText()) + '">Stats</abbr>'>>
    
    Which requires the wikifyToString() function: (put it in a script tagged passage)
    /*
    	wikifyToString(markup)
    
    	Wikifies the given markup and returns its string representation with
    	all HTML-special-characters encoded as entities/references.
    
    	Caution!  Using this with markup which results in anything other than,
    	basically, plain text will produce suboptimal results.
    */
    window.wikifyToString = function (markup) {
    	var buffer = document.createElement("div");
    	new Wikifier(buffer, markup);
    	return buffer.innerHTML.replace(/["']/g, function (c) {
    		return c === '"' ? "&quot;" : "&#039;";
    	});
    };
    
  • Wikifytostring worked like a charm, damn it's good. It has some minor limitations, such as it doesn't seem to be able to handle line breaks so I couldn't cut things into paragraphs (even if I wanted to, which currently doesn't bother me), but overall it looks fantastic.

    EncodeSpecial also looks useful and I should be able to get some mileage out of it.

    One last thing that's mostly just for my own education/curiosity - what is the "wiki" and what does "wikify" mean?
  • Charisma wrote: »
    Wikifytostring worked like a charm, damn it's good. It has some minor limitations, such as it doesn't seem to be able to handle line breaks so I couldn't cut things into paragraphs (even if I wanted to, which currently doesn't bother me), but overall it looks fantastic.
    Its notes do say that non-plain text produces suboptimal results. Line breaks do not transform into plain text. Ergo….

    How would you want them handled?

    Charisma wrote: »
    One last thing that's mostly just for my own education/curiosity - what is the "wiki" and what does "wikify" mean?
    In the sense which I used it, to wikify means to transform into a wiki. SugarCube's markup transformation engine is based on TiddlyWiki (v1.2.39).
  • edited September 2015
    Its notes do say that non-plain text produces suboptimal results. Line breaks do not transform into plain text. Ergo….

    How would you want them handled?

    I can't foresee needing line breaks, it seems like it would be way too involved to ask someone else to code it for me.
    In the sense which I used it, to wikify means to transform into a wiki. SugarCube's markup transformation engine is based on TiddlyWiki (v1.2.39).

    I'm sure my utter ignorance is showing, but what's a wiki?
  • Charisma wrote: »
    I'm sure my utter ignorance is showing, but what's a wiki?
    I think I'll let Wikipedia answer this one (see: Wiki at Wikipedia).
Sign In or Register to comment.