Small Site Generator

This program allow you to generate a simple site structure from any list of detached web pages.

Generating a Color List

<< Graphics`Colors`

AllColors

\(\(ExaLetters = {"A", "B", "C", "D", "E", "F"};\)\)

ToExa[i_] :=  
        If[i < 10, ToString[i], ExaLetters[[i - 9]]];

To256[i_] := 
        If[i < 0, "00", 
          If[i > 255, "FF", 
            ToExa[Floor[i/16]] <> ToExa[Mod[Floor[i], 16]]]];

ColorConverter[RGBColor[r_, g_, b_]] :=  "#" <> To256[r*256] <> 
        To256[g*256] <> To256[b*256]

Un esempio di conversione.

ColorConverter[YellowOchre]

Available Colors.

\(\(\(LengthAllColors = Length[AllColors];\)\(
\)
    \)\)
\(\(PrintAllColors[] := 
        For[i = 1, i <= LengthAllColors, \(++i\), 
	
          col = AllColors[\([i]\)]; 
	val = ToExpression[col]; 
	
          Print[col]; 
          Show[Graphics[RasterArray[{{val}}]], AspectRatio -> 1/50, 
            ImageSize -> {500, 20}]];\)\)

Introducing the Program and its parameters.

This Notebook is a program build a "flat" web site of n pages. It requires in input: 
- a folder with n web page, P1.html, ..., Pn.html, or less, with no link pointing to them (they many contain any number of links to other pages, though); 
- n titles ("P1", "P2", ..., "Pn") for the n pages; if a page does not exist it is created empty;
- two more pages with a fixed name: header.html and footer.html. 
Each new page has a menu with a link to all pages of the site, including the page itself (which is outlined).  Each  new page has header and footer the two extra pages.

We set the address of the folder.

SetDirectory["C:\Documents and \
Settings\stefano\Documenti\Tarski"]

We supply the list of titles for the pages of the site.

\(TitleList = 
{"Introduction to Tarski Game Semantic", 
"Adding Irreversible Backtracking", 
"Adding Reversible Backtracking", 
"A Semantic of Constructive Implication", 
"A Semantic of Cut", 
"A Program implementing Tarski Games with backtracking"}\)

This list was used for the web site of "Types 2003":

TitleList={
"Introduction To Types 2003",
"Invited Speakers",
		"ListOfPartecipants",
			"Schedule Of The Congress",
"How To Register",
"How To Reserve", 
"How To Submit A Talk",
"Social Events", 
"Call For Papers",
"How To Get To Villa Gualino",
"Map Of The Town Of Turin",
"Picture Of Villa Gualino",
	"Organizing Commitee"
	}

This other list was used for the web site on "Game Semantic":

TitleList=
  {"Introduction to Tarski Game Semantic",
    "Adding Irreversible Backtracking",
    "Adding Reversible Backtracking",
    "A Semantic of Constructive Implication",
    "A Semantic of Cut",
    "A Program implementing Tarski Games with backtracking"}

We decide the color for the Menu, the Header, the Footer, and for an active button of the Menu.

SiteColor  = "#DDEEFF";

MenuColor  =  ColorConverter[Gold]; 

    MenuColorInside  =  
        ColorConverter[LightGoldenrod];

HeaderColor  =  ColorConverter[SkyBlue]; 
FooterColor  =  ColorConverter[SkyBlue];

HigthLightColor = 
        ColorConverter[Gainsboro];
HigthLightColor2 = ColorConverter[LightBlue];

We decide how many pixel should be large the Menu, how many the text within each page, and back/forth points in the footer.

WidthMenu  =  200;
WidthText  =  750;

BackPointerWidth  =  266;

We generate the list FileList of source files for the site: they consists of the name of the arguments with all spaces removed. The list of final names for site files is then obtained by adding a numeration the source files. File number 1 becomes the index of the site.

TitleNumber  =  Length[TitleList]

FileList = 
      Table[StringReplace[
            TitleList[[i]], {" " ->  ""}] <> ".html", {i, 
          1  TitleNumber}]

NumberToString[i_] := 
      If[i < 10, "0" <> ToString[i], ToString[i]]

PageList = 
      Table[NumberToString[i] <> "_" <> FileList[[i]], {i, 1, 
          TitleNumber}]

Generating Tables in Html.

The program requires subroutines dealing with color tables in Html.

Border  =  1;
Border2  =  0;

\(\(HtmlHTable[list_, color_String, \ border_] := 
        StringJoin[\ 
		"<table border = ", 
		ToString[
            border], 
		" BGcolor = ", 
		color, \ 
		"> <tr> 
 ", \ \ 
		\ Table[{	"<td> 
", 
              list[\([i]\)], 	"</td> 
"}, 
			\ \ \ \ \ \ \ \ \ \ {i, 1, Length[list]}], 
		\ "</tr> </table>"];\)\)

\(\(HtmlHTable[list_, width_Integer, \ border_] := 
        StringJoin[
		\ "<table border = ", 
		ToString[
            border]\ , \ 
			" width = ", \ \ 
		ToString[
            width], 
			"> <tr> 
", 
		\ Table[{	"<td valign = top > 
", 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ list[\([i]\)], 	
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "</td> 
"}, 
			\ \ \ \ \ \ \ \ \ \ {i, 1, Length[list]}], 
		\ "</tr> </table>"];\)\)

\(\(HtmlVTable[list_, \ border_] := 
        StringJoin[
		"<table border = ", 
		ToString[
            border], 
			" > 
", \ 
		Table[
			{	"<tr> <td> 
", 
				list[\([i]\)], 	
					"</tr> </td> 
"}, 
			{i, 1, Length[list]}], \ "</table>"];\)\)

\(\(HtmlVTable[list_, width_, \ color_, \ border_] := 
	
        StringJoin[\ 
		"<table border = ", 
		ToString[
            border], 
		" width = ", \ \ 
		ToString[
            width], 
		" BGcolor = ", 
		color, \ \ 
			">  
", 
	Table[{	"<tr> <td> 
", 
              list[\([i]\)], 	"</tr> </td> 
"}, 
			{i, 1, 
              Length[list]}], \ "</table>"];\)\)

\(\(HtmlVTable[list_, width_, \ color_, color2_, exception_, 
          exceptioncolor_, \ border_] := 
	
        StringJoin[\ 
		"<table border = ", 
		ToString[
            border], 
		" width = ", \ \ 
		ToString[
            width], 
		" BGcolor = ", 
		color, \ \ 
			">  
", 
	Table[
            If[i == exception, 
					{	"<tr> <td bgcolor = ", 
						\ exceptioncolor, 
							"> 
", 
						list[\([i]\)], 	
\ \ \ \ \ \ \ \ \ \ \ \ "</tr> </td> 
"}, 
					{	"<tr> <td bgcolor = ", 
						\ color2, 
							"> 
", 
list[\([i]\)], 	
"</tr> </td> 
"}], 
			{i, 1, 
              Length[list]}\ ], \ 
			"</table>"];\)\)

Generating A Small Web Site

We define the program and feed it the right inputs.

HtmlMenu[MenuList_, width_, i_, col_] := 
        HtmlVTable[
          Prepend[MenuList, "<H2 > <center> Menu </center> </H2> "], 
          width,  MenuColor, MenuColorInside,  i, col,  
          Border];

\(\(Page[i_, \ Header_, Footer_, Menu_, Title_, Text_, widthmenu_, \ 
          widthtext_] := 
CompoundExpression[
			SiteBody := 
            HtmlHTable[
				{Menu\ , 
				\ \ StringJoin["<center> <H2>", \ 
						ToString[
                    i], \ 
						\ ". ", 
						\ Title, \ 
						\ "</H2> </center> <BR>", 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Text]
			}, 
				widthmenu\  + \ 
                widthtext, 
			Border]; 
			HtmlVTable[{HtmlVTable[{StringJoin["<body bgcolor = ", \ 
                    SiteColor, \ "> 
 ", \ 
                    Header]}, 
widthmenu\  + \ 
                  widthtext\ , 
HeaderColor, \ 
                Border], 
SiteBody, 
HtmlVTable[
{Footer}, 
widthmenu\  + \ 
                  widthtext\ , 
FooterColor, \ Border]}, \ 
            Border2]
		];\)\)

Here is a program generating an empty new file of a given name, and returning its content (the empty list). If the file already exists it is cleared.

\(\(NewFile[path_] := 
        CompoundExpression[
		stream = OpenWrite[path]; 
		Close[
            stream]; 
		{}];\)\)

We define a function copying the content of a list from a file, and creating the file if it does not exists.

\(\(StringFromFile[path_] := 
        If[
		MemberQ[FileNames[], path], 
          StringJoin[ReadList[path, String]], 
		NewFile[
            path]];\)\)

H := StringFromFile["Header.html"];

F := StringFromFile["Footer.html"];

\(\(MenuList[TitleList_, FileList_] := \ 
        Table[
		{" <A HREF = "", \ 
			FileList[\([i]\)], \ 
				" ">  ", \ 
			ToString[
              i], \ 
			". ", \ 
			TitleList[\([i]\)], \ 
			" </A>"}, 
		{i, 1, Length[TitleList]}];\)\)

Previous[i_, n_] := Mod[i - 2, n] + 1;
Next[i_, n_] := Mod[i, n] + 1;

\(BackAndForth[i_, n_, TitleList_, PageList_, \ col_, \ col2_] := 
      StringJoin[
		
\ \ \ \ \ "<table align = center border = 1> <tr>", 

	\ \ \ "<td width = ", 
			ToString[
          BackPointerWidth], 				
		" bgcolor = ", 
		col2, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ "> <B> Prev.: </B> <A HREF="", 
		PageList[\([Previous[i, 
              n]]\)], 
		"">  ", 
			ToString[
          Previous[i, 
            n]], 
		". ", 
		TitleList[\([Previous[i, 
              n]]\)], 
		" </A>. </td> 
", 
		
		" <td width =", 
		ToString[
          BackPointerWidth], 
		" bgcolor = ", 
		col, 		
		"> <B> Up: </B> <A HREF="", 
		PageList[\([i]\)], 
		"">  ", 
		ToString[
          i], 
		". ", 
		TitleList[\([i]\)], 
		"</A>. </td>  
", 
		
		"<td width = ", 
			ToString[
          BackPointerWidth], 		
	\ \ \ \ \ " bgcolor = ", 
		col2, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ "> <B> Next: </B> <A HREF="", 
		PageList[\([Next[i, 
              n]]\)], 
		""> ", 
			ToString[
          Next[i, n]], 
		". ", 
		TitleList[\([Next[i, 
              n]]\)], 
			" </A>. </td> 
", 
		
		"</tr> </table>"
	]\)

\(WebSite[TitleList_, FileList_, PageList_, \ widthmenu_, \ 
        widthtext_] := \ 
      CompoundExpression[
		n = Length[TitleList]; 
		For[i = 1, 
          i <= n, \(++i\), 
			page = 
            OpenWrite[PageList[\([i]\)]]; 
			WriteString[
            page, 
				Page[
              i, 
					H, 
					StringJoin[
                BackAndForth[i, n, TitleList, PageList, \ HigthLightColor, 
                  HigthLightColor2], F], 
					HtmlMenu[
                MenuList[TitleList, PageList], widthmenu, \ i + 1, \ 
                HigthLightColor], 
					TitleList[\([i]\)], 
					StringFromFile[FileList[\([i]\)]], 
			widthmenu, \ 
              widthtext]]; \ 
			Close[
            page];]
	\ \ \ \ \ \ \ \ \ \ ]\)

We clean up all previous generated files starting with "xy_" for some digits x, y.

\(\(NumberedFile[name_] := 
        And[
		DigitQ[
            StringTake[name, 2]], 
		StringTake[
              name, {3}] == "_"];\)\)

DeleteNumberedFiles[] := 
        DeleteFile[Select[FileNames[], NumberedFile]];

DeleteNumberedFiles[]

We generate the new site.

WebSite[TitleList, FileList, PageList,  WidthMenu,  
      WidthText]

We duplicate the first file of the site using the canonical name "index.html".

stream  =  OpenWrite["index.html"];

WriteString[stream, StringFromFile[PageList[[1]]]];
Close[stream];