Script to XHTML

I wrote my own script to convert the selection (or whole front document) in Script Editor to XHTML. It uses the same CSS tags as Jonathon’s program, but does not add the style data in, unless a property is set. I’ve also got the Source Code for his program, so it will be interesting to see similarities.

  1     (*
  2     Script to XHTML v1.0
  3     ©2005 Matthew Schinckel
  4     http://schinckel.net/
  5     
  6     Converts selection (or whole document) into XHTML code.
  7     
  8     Insert the StyleSheet into your CSS if you want.  Else set externalCSS to false.
  9     
 10     Put it in your Scripts Menu, and it will work a treat for you.  
 11     Even copies the data to the clipboard.
 12     
 13     Bugs/Issues:
 14     
 15     • Script Editor reports the start of comments [--] as being black, not grey.
 16     • Doesn't handle references.
 17     • Operators and values are treated all as values.
 18     
 19     *)
 20     
 21     property StyleSheet : "
 22     .AppleScript { background-color:#ffffff; border: solid black 1px; padding:0.5em 1em 1em 1em; text-align:left; font-family: Verdana,Sans-Serif; overflow:auto; font-size:0.9em; white-space:pre; line-height:1.2em; margin-bottom:9px;}
 23     .as_new_text  { font-family:Courier; color: purple; }
 24     .as_operators  { color: black; }
 25     .as_language  { font-weight: bold; color: blue; }
 26     .as_application  { color: blue; }
 27     .as_comments, .as_comment  { font-style:italic; color: gray; }
 28     .as_values  { color: black; }
 29     .as_variables ,.as_variable { color: green; }
 30     .as_references  { color: purple; }
 31     "
 32     property externalCSS : true
 33     
 34     -- The HTML code either side of the block.
 35     property htmlStart : "<pre class='AppleScript'>" & return
 36     property htmlEnd : return & "</pre>" & return
 37     
 38     property tagStart : "<span class='"
 39     property tagMiddle : "'>"
 40     property tagEnd : "</span>"
 41     
 42     -- Apparently, this one is different to 'return'
 43     property enter : "
 44     "
 45     -- For testing, set this to 2, for use via Script Menu, set to 1.
 46     property win : 1
 47     
 48     if externalCSS is not true then set htmlStart to htmlStart & "<style>" & return & StyleSheet & return & "</style>" & return
 49     
 50     tell application "Script Editor"
 51         set theData to contents of selection of document of window win
 52         if (theData is "") then
 53             -- No selection, let's do the whole document.
 54             set textList to attribute run of contents of document of window win
 55             set fontList to font of attribute run of contents of document of window win
 56             set colorList to color of attribute run of contents of document of window win
 57         else
 58             set textList to attribute run of contents of selection of document of window win
 59             set fontList to font of attribute run of contents of selection of document of window win
 60             set colorList to color of attribute run of contents of selection of document of window win
 61         end if
 62     end tell
 63     
 64     set html to ""
 65     
 66     repeat with i from 1 to the count of textList
 67         set theClass to whichClass(item i of fontList, item i of colorList)
 68         set theText to my HTMLify(item i of textList)
 69         set html to html & tagStart & theClass & tagMiddle & theText & tagEnd
 70     end repeat
 71     
 72     set the clipboard to htmlStart & html & htmlEnd
 73     beep
 74     
 75     -- Utility Functions
 76     
 77     on whichClass(theFont, theColor)
 78         -- Set the class according to the font or colour.
 79         if theFont is "Verdana-Bold" then return "as_language"
 80         if theColor is {16384, 32768, 0} then return "as_variables"
 81         if theColor is {19660, 19960, 19960} then return "as_comments"
 82         if theColor is {0, 0, 65535} then return "as_application"
 83         if theColor is {32768, 0, 32768} then return "as_new_text"
 84         return "as_values"
 85     end whichClass
 86     
 87     on replace(theText, find, replace)
 88         -- Nice replace function
 89         set OldDelims to AppleScript's text item delimiters
 90         set AppleScript's text item delimiters to find
 91         set newText to text items of theText
 92         set AppleScript's text item delimiters to replace
 93         set theResult to newText as text
 94         set AppleScript's text item delimiters to OldDelims
 95         return theResult
 96     end replace
 97     
 98     on HTMLify(someText)
 99         -- This might need some more entries.
100         -- Perhaps a better way of doing it...?
101         set someText to replace(someText, "&", "&amp;")
102         set someText to replace(someText, "\"", "&quot;")
103         set someText to replace(someText, "<", "&lt;")
104         set someText to replace(someText, ">", "&gt;")
105         --set someText to replace(someText, tab, "&nbsp;&nbsp;&nbsp;&nbsp;")
106         --set someText to replace(someText, enter, "<br />" & enter)
107         --set someText to replace(someText, return, "<br />" & return)
108         return someText
109     end HTMLify
blog comments powered by Disqus