Skip to content

Instantly share code, notes, and snippets.

@minoki
Created August 17, 2009 13:34
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 minoki/169119 to your computer and use it in GitHub Desktop.
Save minoki/169119 to your computer and use it in GitHub Desktop.
require "cocoa"
local filename = arg[1]
if not filename then
return
end
local ishtml = false
if filename:match("%.html$") then
ishtml = true
end
local filepath = cocoa.NSString:stringWithUTF8String_(filename)
local fileurl = cocoa.NSURL:fileURLWithPath_(filename)
local options = cocoa.NSXMLDocumentTidyXML
if ishtml then
options = cocoa.NSXMLDocumentTidyHTML
end
local xmldoc = cocoa.NSXMLDocument:alloc():initWithContentsOfURL_options_error_(fileurl,options)
local scripts = xmldoc:nodesForXPath_error_(".//script[@src]")
for x in scripts:objectEnumerator() do
local src = x:attributeForName_("src"):stringValue()
x:removeAttributeForName_("src")
local srcpath = filepath:stringByDeletingLastPathComponent()
:stringByAppendingPathComponent_(src)
local code,err = cocoa.NSString:stringWithContentsOfFile_encoding_error_(srcpath,cocoa.NSUTF8StringEncoding,nil)
local textNode = cocoa.NSXMLNode:textWithStringValue_(code)
x:addChild_(textNode)
end
local styles = xmldoc:nodesForXPath_error_(".//link[@rel][@href]")
for x in styles:objectEnumerator() do
local rel = x:attributeForName_("rel"):stringValue()
if rel:UTF8String():find("stylesheet") then
local href = x:attributeForName_("href"):stringValue()
local srcpath = filepath:stringByDeletingLastPathComponent()
:stringByAppendingPathComponent_(href)
local code,err = cocoa.NSString:stringWithContentsOfFile_encoding_error_(srcpath,cocoa.NSUTF8StringEncoding,nil)
local elem = cocoa.NSXMLNode:elementWithName_stringValue_("style",code)
local attr = cocoa.NSXMLNode:attributeWithName_stringValue_("type","text/css")
elem:addAttribute_(attr)
x:parent():replaceChildAtIndex_withNode_(x:index(),elem)
end
end
local data = xmldoc:XMLDataWithOptions_(0)
data:writeToFile_atomically_("output.xhtml",true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment