Skip to content

Instantly share code, notes, and snippets.

@shawngraham
Created February 26, 2018 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawngraham/20c0da9725db90a9c2225716494de4d0 to your computer and use it in GitHub Desktop.
Save shawngraham/20c0da9725db90a9c2225716494de4d0 to your computer and use it in GitHub Desktop.
window.storyFormat({
"name": "Twison",
"version": "0.0.1",
"author": "Mike Lazer-Walker",
"description": "Export your Twine 2 story as a JSON document",
"proofing": false,
"source": "<html>\r\n\t<head>\r\n\t\t<title>{{STORY_NAME}}</title>\r\n\t\t<script type=\"text/javascript\">\r\n/**\r\n * Twison - Twine 2 JSON Export Story Format\r\n * \r\n * Copyright (c) 2015 Mike Walker\r\n * https://lazerwalker.com\r\n *\r\n * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and\r\n * associated documentation files (the \"Software\"), to deal in the Software without restriction,\r\n * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,\r\n * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,\r\n * subject to the following conditions:\r\n *\r\n * The above copyright notice and this permission notice shall be included in all copies or substantial\r\n * portions of the Software.\r\n *\r\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT\r\n * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r\n * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r\n * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\r\n * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r\n */\r\nvar Twison={extractLinksFromText:function(t){var n=t.match(/\\[\\[.+?\\]\\]/g);return n?n.map(function(t){var n=t.match(/\\[\\[(.*?)\\-\\&gt;(.*?)\\]\\]/);return n?{name:n[1],link:n[2]}:(t=t.substring(2,t.length-2),{name:t,link:t})}):void 0},convertPassage:function(t){var n={text:t.innerHTML},a=Twison.extractLinksFromText(n.text);if(a&&(n.links=a),[\"name\",\"pid\",\"position\",\"tags\"].forEach(function(a){var e=t.attributes[a].value;e&&(n[a]=e)}),n.position){var e=n.position.split(\",\");n.position={x:e[0],y:e[1]}}return n.tags&&(n.tags=n.tags.split(\" \")),n},convertStory:function(t){var n=t.getElementsByTagName(\"tw-passagedata\"),a=Array.prototype.slice.call(n).map(Twison.convertPassage),e={passages:a};[\"name\",\"startnode\",\"creator\",\"creator-version\",\"ifid\"].forEach(function(n){var a=t.attributes[n].value;a&&(e[n]=a)});var i={};return e.passages.forEach(function(t){i[t.name]=t.pid}),e.passages.forEach(function(t){t.links&&t.links.forEach(function(t){t.pid=i[t.link]})}),e},convert:function(){var t=document.getElementsByTagName(\"tw-storydata\")[0],n=JSON.stringify(Twison.convertStory(t),null,2);document.getElementById(\"output\").innerHTML=n}};window.Twison=Twison;\t\t\r\n\t\t</script>\r\n\t</head>\r\n\t<body>\r\n\t\t<pre id=\"output\">\r\n\t\t\r\n\t\t</pre>\r\n\t\t<div id=\"storyData\" style=\"display: none;\">\r\n\t\t\t{{STORY_DATA}}\r\n\t\t</div>\r\n\t\t<script>\r\n\t\t\tTwison.convert()\r\n\t\t</script>\r\n\t</body>\r\n</html>"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment