Created
November 26, 2011 16:03
-
-
Save maul-esel/1395905 to your computer and use it in GitHub Desktop.
files to dynamically generate a CHM for ahkbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; requires latest AHK_L | |
#SingleInstance force | |
SetWorkingDir %A_ScriptDir% | |
; ============= config ============= | |
build_dir := A_ScriptDir "\build" | |
user := "maul-esel" | |
; ================================== | |
PrepareBuildDir() ; prepare build dir | |
src := ComObjCreate("MSXML2.DOMDocument") ; represents the html source | |
src.Load(download("http://" . user . ".github.com/ahkbook/index.html", build_dir)) ; download index + load | |
src_index := src.selectNodes("//ul").item(1) ; get language list (2nd <ul>) | |
dest := ComObjCreate("MSXML2.DOMDocument") ; represents the hhc destination | |
dest.LoadXML(template()) ; load template | |
dest_index := dest.selectSingleNode("//ul") ; get item list (1st <ul>) | |
src_list := src_index.selectNodes("li") ; get list of languages | |
Loop % src_list.length | |
{ | |
AddLanguage(src_list.item(A_Index - 1), dest_index) ; add languages to dest_index | |
} | |
dest.save(A_ScriptDir "\contents.hhc") ; save hhc | |
retransform(A_ScriptDir "\contents.hhc") ; add HTML header (would break XML) | |
Run %Comspec% /c "cd "%A_ScriptDir%" && "C:\Program Files\HTML Help Workshop\hhc.exe" "%A_ScriptDir%\ahkbook.hhp" > log.txt", %A_ScriptDir%, Hide ; compile | |
MsgBox finished! | |
Run log.txt ; show log to user | |
FileRemoveDir %build_dir%, 1 ; clean build dir | |
return | |
; ======================================= end of auto-execute section =============================================== | |
/* | |
Prepares the build dir: | |
* empty it | |
* create empty.html | |
*/ | |
PrepareBuildDir() | |
{ | |
global build_dir | |
IfExist %build_dir% | |
FileRemoveDir, %build_dir%, 1 | |
FileCreateDir, %build_dir% | |
FileAppend <!DOCTYPE html><html><head><title>ahkbook - page missing</title></head><body></body></head>, %build_dir%\empty.html | |
} | |
/* | |
Adds a language and all its topics | |
src_item - the XmlDomElement representing the language in index.html | |
dest_parent - the XmlDomElement the language should be appended to (the HHC index) | |
*/ | |
AddLanguage(src_item, dest_parent) | |
{ | |
global user, build_dir | |
dest_item := AddItem(src_item, dest_parent) ; add the language itself to the hhc index | |
link := src_item.selectSingleNode(".//a/@href").value ; get the link to the language's introduction | |
lang_folder := SubStr(link, 1, InStr(link, "/") - 1) ; get the language's subfolder | |
lang := ComObjCreate("MSXML2.DOMDocument") ; represents the language's introduction document | |
lang.Load(download("http://" . user . ".github.com/ahkbook/" link, build_dir)) | |
lang_index := lang.selectSingleNode("//ol").selectNodes("li") ; get the source index list of the language's topics | |
if (lang_index.length > 0) ; if topics exist: | |
dest_list := dest_item.appendChild(dest_item.ownerDocument.createElement("ul")) ; prepare the destination item | |
Loop % lang_index.length | |
{ | |
AddItem(lang_index.item(A_Index - 1), dest_list, lang_folder) ; add all topics for this language | |
} | |
} | |
/* | |
Adds a topic to the HHC index | |
src_item - the XmlDomElement representing the language in index.html or the language's index | |
dest_parent - the XmlDomElement the language should be appended to (in the HHC index) | |
lang - the language subfolder the topic belongs to | |
*/ | |
AddItem(src_item, dest_parent, lang = "") | |
{ | |
global user, build_dir | |
link := src_item.selectSingleNode(".//a/@href").Value ; get the relative topic link | |
name := src_item.selectSingleNode(".//a").text ; get the topic title | |
link := link ? ("http://" . user . ".github.com/ahkbook/" . (lang ? lang . "/" : "") . link) : "" ; if the link is not empty: make absolute, else set to "" | |
dest_item := dest_parent.appendChild(getElement(dest_parent.ownerDocument, name, download(link, build_dir, false, lang))) ; append a new item to the dest_parent item | |
sub := src_item.selectNodes("ul/li") ; get a list of subtopics | |
if (sub.length > 0) ; if subtopics exist: | |
dest_sub := dest_item.appendChild(dest_item.ownerDocument.createElement("ul")) ; prepare the destination item | |
Loop % sub.length | |
{ | |
AddItem(sub.item(A_Index - 1), dest_sub, lang) ; add all subtopics | |
} | |
return dest_item | |
} | |
/* | |
creates a list item | |
doc - the XmlDomDocument to use | |
name - the item's topic name | |
path - the local path to the topic | |
*/ | |
getElement(doc, name, path) | |
{ | |
item := doc.createElement("li") | |
obj := item.appendChild(doc.createElement("object")) | |
obj.setAttribute("type", "text/sitemap") | |
param1 := obj.appendChild(doc.createElement("param")) | |
param1.setAttribute("name", "Name") | |
param1.setAttribute("value", name) | |
param2 := obj.appendChild(doc.createElement("param")) | |
param2.setAttribute("name", "Local") | |
param2.setAttribute("value", path) | |
return item | |
} | |
; removes the <!DOCTYPE ...> from a file | |
transform(file) | |
{ | |
FileRead t, %file% | |
FileDelete %file% | |
FileAppend, % RegExReplace(t, "Ui)^\s*<!DOCTYPE (.*)>\s*\r?\n"), %file% | |
} | |
retransform(file) | |
{ | |
FileRead t, %file% | |
FileDelete %file% | |
FileAppend, <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">`n%t%, %file% | |
} | |
download(url, prefix = "", transform = true, lang = "") | |
{ | |
global build_dir | |
if (url) | |
{ | |
path := prefix . "\" . SubStr(url, 8, StrLen(url)) | |
StringReplace path, path, /, \, 1 | |
SplitPath path,, dir | |
if (!FileExist(dir)) | |
{ | |
FileCreateDir %dir% | |
} | |
UrlDownloadToFile %url%, %path% | |
} | |
else | |
{ | |
path := build_dir "\empty.html" | |
} | |
if (transform) | |
{ | |
transform(path) | |
} | |
return path | |
} | |
/* | |
gets a template for the HHC file | |
*/ | |
template() | |
{ | |
global build_dir, user | |
return "<html>" | |
. "<head><meta name='GENERATOR' content='maul.esel; ahkbook generator script'/></head>" | |
. "<body>" | |
. "<object type='text/site properties'><param name='ImageType' value='Folder'/></object>" | |
. "<ul><li><object type='text/sitemap'><param name='Name' value='ahkbook'/><param name='Local' value='" build_dir . "\" . user . ".github.com\ahkbook\index.html'/></object></li></ul>" | |
. "</body>" | |
. "</html>" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment