Thursday, June 16, 2005

TiddlyWiki hack: exporting tiddler HTML

I was trying to figure out how to export rendered HTML from a TiddlyWiki tiddler so I could paste it into an MS Word document. Since there doesn't seem to be a supported way to do this in TiddlyWiki, I found two methods that seem to work:

  • If you're using Firefox, just highlight the tiddler text, then right-click your selection and choose "View Selection Source." I dunno why this works, but it does.
  • Otherwise, I threw together the Ugliest TiddlyWiki Hack Ever!

I may be reinventing the wheel here, but whatever.

Save a backup of your TiddlyWiki file, then open it in a text editor and search for the createTiddlerToolbar function. In that function, look for the section that adds buttons to the non-editor toolbar and add the following lines to that block:

insertSpacer(theToolbar);
createTiddlyButton(
  theToolbar,
  "html",
  "show/hide HTML of this tiddler",
  onClickToolbarShowHTML
);

Now add the following function somewhere in the page, preferably near where the other button handlers live:

function onClickToolbarShowHTML(e)
{
  hideMessage();
  if(this.parentNode.id) {
    var viewer = 
      document.getElementById("viewer" + this.parentNode.id.substr(7));
    if (!viewer.isHTMLView) {
      viewer.innerHTML = viewer.innerHTML.replace(/\</g,"&lt;");
      viewer.isHTMLView = true;
    } else {
      viewer.isHTMLView = false;
      displayTiddler(
        null,this.parentNode.id.substr(7),1,null,null,false
      );
    }
  }
}

This is admittedly a gross hack: we're just grabbing the HTML of the viewer tiddler and replacing all the left angle brackets with HTML entities, then stuffing the HTML back into the tiddler. Since calling displayTiddler refreshes the tiddler content, we can use that to undo the damage without manipulating the string again.

This has been pretty useful to me, since I love the simple TiddlyWiki table markup for doing spec work, but usually have to cut and paste it into a report written in HTML or Word. Now, I just hit that "html" tiddler button, copy/paste the HTML, then toggle it back.

3 Comments:

At 10:15 AM, Blogger Phil Ringnalda said...

Using view selection source works because it really should be named something odd like "view the sort of source which would produce the DOM that resulted in your selection." Makes it very handy for figuring out what a ton of script constructed, but also results in a never-ending stream of bug reports from people who think it should be the original source, even though something that you select could come from the combination of the HTML and ten different external script files.

 
At 8:11 AM, Blogger Helplessness said...

:offtopic:
Good clues, thanks...

 
At 5:08 PM, Blogger MCAndre said...

The TiddlyWiki framework has changed significantly, so it's not as simply as copying and pasting this code in. Could you update the code and release it as a TiddlyWIki plugin?

 

Post a Comment

<< Home