A Python-and-Awk-Based Web Site Generator
I realize I am starting to build one. Here is the latest bit of machinery: this awk script takes a list of major sections and chapter-level sections, and:
1) Generates an HTML file for each chapter... if that file does not already exist. (Well, here we actually generate "PTML" files, since we have a HTML pre-processor built to include code from file1 in "PTML" file2.) It uses a previously created Python script to put the chapter name in each generated HTML file. element and the
1) Generates an HTML file for each chapter... if that file does not already exist. (Well, here we actually generate "PTML" files, since we have a HTML pre-processor built to include code from file1 in "PTML" file2.) It uses a previously created Python script to put the chapter name in each generated HTML file.
element.
2) Writes out a menu file to access the chapter HTML files, which we use our pre-processor to include into index.html.
#!/usr/bin/awk -f | |
# This program generates the chapter files for DAA. | |
# It reads stdin for the chapter names. | |
BEGIN { | |
indent1 = " " | |
indent2 = indent1 indent1 | |
indent3 = indent2 indent1 | |
indent4 = indent2 indent2 | |
menu = "chap_menu.txt" | |
templ = "ChapTemplate.txt" | |
daa = "Design and Analyis of Algorithms: " | |
create_pg = "../utils/create_page.py" | |
print "" > menu # because we don't want a cumulative menu for each run! | |
} | |
/^$/ { } # blank lines allowed | |
/^\;/ { } # allows comments in the chapter file | |
/^[IVXCM]/ { # a major section name | |
sect_nm = split_on_caps($2) | |
print indent2 "">> menu | |
print indent3 $1 ". " sect_nm >> menu | |
print indent2 " " >> menu | |
} | |
/^[0-9]/ { # this is a chapter name | |
chap_file = $2 ".ptml" # we process ptml files into html | |
chap_html = $2 ".html" # we process ptml files into html | |
chap_nm = split_on_caps($2) | |
if (system( "[ -f " chap_file " ] ") == 0) | |
print chap_file " already exists." | |
else | |
system("touch " chap_html) | |
system(create_pg " <" templ " >" chap_file " \"" daa chap_nm "\"") | |
print indent3 "">> menu | |
print indent4 "\"" chap_html "\">" >> menu | |
print indent4 $1 ". " chap_nm >> menu | |
print indent4 "" >> menu | |
print indent3 " " >> menu | |
} | |
function split_on_caps(s) { | |
split_str = "" | |
split(s, chars, "") | |
for(i = 0; i < length(chars); i++) | |
{ | |
# i > 0: no need to introduce space at start of string! | |
if((i > 0) && match(chars[i], /[A-Z]/)) | |
split_str = split_str " " | |
split_str = split_str chars[i] | |
} | |
return split_str | |
} |
2) Generates a menu file linking to the existing or just-generated HTML chapter pages, which our HTML pre-processor can include in index.html.
UPDATE: Thanks to Ken B. for noting that something weird happened with the first publication of this post.
Scripts seem to be missing here, looks like something got truncated.
ReplyDeleteA lot of snakes generate Websites.
Yes, something weird happened here! Post clearly truncated. Will fix, thanks.
Delete