I started creating a html class for learning purposes and also for utility, because i needed it in two scripts. Also, this is the first thing like his i create so i will learn something new.
It wont be a lot complex. Im posting this for receiving some feedback or critiques, suggestions, now that i didnt write a lot of code. Here is de design of the class.
Also, it dont take a lot of time. The hardest thing is the wTable, and maybe the Form method, but not last long if you know regexp. Im wondering if doing that with tables or not...
####################
#RGSS HTML DOCUMENT#
####################
This object represents a html page in memory. You can open it, write, format
and finally save to a file.
You only can write to the document throught the write methods, but you
have a plenty of them, and a few shortcuts and automtism.
For last, the html code is in a single string called contents, that can
only be readed. The save in a html file is done at the end, calling a closing
method.
CREATING A NEW HTMLDOC OBJECT
The constructor needs the html filename (accepts path, but it puts
automatically the extension .html when saving it), and accepts two extra
parameters:
title: you can pass the title of the HTML page. If you do, it will be called
the wHead method and it will asigned automatically
bgcolor: in the same way, you can set the bgcolor of the body here. This
will call the wBody method.
Note that if you pass these two arguments, the creation of the starting part
of the document is more quick.
ATRIBUTES
You can set or get any of these atributes:
(text atributes in html)
-b
-u
-s
(font atributes in html)
-size
-name
-color
(background colors for page or other things in html)
-bgcolor
(the file where it will be sav ed)
-filename
You can also read the string contents, that have all the html code.
FORMATING
This object writes formated code using his owns previsously named atributes as
a marks. So, if you have the b atribute to true, it will write each
time you write a text. It also writes all the font information that is font name,
color and size.
The way for formating a htmldoc is to changue these values each time you need
to modifiy the text format. But, for this task you have two options:
A. You can use the atributes way, and just modifiy each time setting they.
B. You can use the format method(Form) that interprets a string to changue the
state of the format atributes. So, for example if you call:
doc.Form 'b,c,13,#red,Arial'
You are changuing all these atributes. The b, c, u, etc work in a mechanism way,
so if its true it will be false, and if false, it will be true. The number is
for the font size.
Note that font colors use the special code # to mark them and bg colors use @
TEMPORAL FORMATING
Using this method(tFom) you can changue any atributes temporally, so that
they only will be aplied to the next x write methods(default=1).
doc.tForm 'b c 13 #red Arial', 3
GENERAL WRITE METHODS
These are. They are abreviated like wX
wLine: Writes a string of text and appends a final <br>
wH: Writes a formated header. Accepts a string name and a addiotional integer value 1-6. Note
that font size formating is skiped here. Default=2.
wTxt: Writes a formated string of text.
wList: Writes a list based on a given array. The natural is a string array,
but it forces the to_s of the object so if you have these implemented, it will
work.
You can pass two extra parameters:
type: 'dd', 'ul', 'ol' for each of the posible html lists. Default is ul.
style: 'square' or 'i'(and others) for each of the posible graphics for the lists. Default are
1, circle.
wBr: writes a single <BR>. You can also set and extra numeric argument so
it writes all that BR's.
wHr: Like the Wbr, but with <hr>
wTable: This method can write a table showing the values of:
-RGSS table class
-A array that contents other arrays with simplier numerics or strings
-A array of equals structs or hashes
-A array of equals arrays, setting the rows tags apart
-A array of equals objects, while they have a valid to_a method
Note that method can define the most important configurations that html suport,
like cellspacing, widht, etc.
wP(text, align): You can write a new paragraph with setting the alignement
for it. The default align is left.
wLink(link, description): Writes a html link with optional description text.
If not passed, description is the link dir.
wImg(img, width, height): Writes a image, and also you can set optionally
the width and height. Default values are 100
STARTING METHODS
wHead(title): This method writes all the header. You have to pass
a optional string for the title of the page. The default is ''
wBody(bgcolor): This starts the body, fixing the bgcolor. If not bgcolor
is passed, it will use the atribute defined one.
Note that is better to not use this methods and send this options in the
constructor.
ENDING AND SAVING METHODS
EndSave: Use this method if you just want to end a html document and save
it directly to the file. No other uses for it.
wEnd: This writes the ending of html, but it dont save it.
Save: This save the document to the specified file.
NON FORMATED TEXT AND EXTRA CODE
If you want to show a non formated text, or a add a completely yours thing
to the code, or make any other special thing, you have to use the write code
method(wCode)
This dont add any extra code to the string you pass, so its a clean way of adding
some things to the contents.
ADDING SPECIAL FORMATINGS
The way of adding extra formating is calling wTxt sending the text with
the special html tags. Example:
doc.wTxt [sub] example text [/sub]
EXAMPLE
doc = HtmlDoc.new 'log', 'Title: You can set title through constructor also',
'black'
doc.b=true # Set the formating b to true
doc.wH 'Combat against KL', 1 #Writes a html h1
doc.b=false
doc.Form '20' # Changues the font size to 20
doc.wLine 'Testing line' # Adds a <br> at the end
doc.wList ['wep', 'wea', 'yu'] # Show a htl list with that entries
doc.SaveEnd # End html doc and save into the html file
It wont be a lot complex. Im posting this for receiving some feedback or critiques, suggestions, now that i didnt write a lot of code. Here is de design of the class.
Also, it dont take a lot of time. The hardest thing is the wTable, and maybe the Form method, but not last long if you know regexp. Im wondering if doing that with tables or not...
####################
#RGSS HTML DOCUMENT#
####################
This object represents a html page in memory. You can open it, write, format
and finally save to a file.
You only can write to the document throught the write methods, but you
have a plenty of them, and a few shortcuts and automtism.
For last, the html code is in a single string called contents, that can
only be readed. The save in a html file is done at the end, calling a closing
method.
CREATING A NEW HTMLDOC OBJECT
The constructor needs the html filename (accepts path, but it puts
automatically the extension .html when saving it), and accepts two extra
parameters:
title: you can pass the title of the HTML page. If you do, it will be called
the wHead method and it will asigned automatically
bgcolor: in the same way, you can set the bgcolor of the body here. This
will call the wBody method.
Note that if you pass these two arguments, the creation of the starting part
of the document is more quick.
ATRIBUTES
You can set or get any of these atributes:
(text atributes in html)
-b
-u
-s
(font atributes in html)
-size
-name
-color
(background colors for page or other things in html)
-bgcolor
(the file where it will be sav ed)
-filename
You can also read the string contents, that have all the html code.
FORMATING
This object writes formated code using his owns previsously named atributes as
a marks. So, if you have the b atribute to true, it will write each
time you write a text. It also writes all the font information that is font name,
color and size.
The way for formating a htmldoc is to changue these values each time you need
to modifiy the text format. But, for this task you have two options:
A. You can use the atributes way, and just modifiy each time setting they.
B. You can use the format method(Form) that interprets a string to changue the
state of the format atributes. So, for example if you call:
doc.Form 'b,c,13,#red,Arial'
You are changuing all these atributes. The b, c, u, etc work in a mechanism way,
so if its true it will be false, and if false, it will be true. The number is
for the font size.
Note that font colors use the special code # to mark them and bg colors use @
TEMPORAL FORMATING
Using this method(tFom) you can changue any atributes temporally, so that
they only will be aplied to the next x write methods(default=1).
doc.tForm 'b c 13 #red Arial', 3
GENERAL WRITE METHODS
These are. They are abreviated like wX
wLine: Writes a string of text and appends a final <br>
wH: Writes a formated header. Accepts a string name and a addiotional integer value 1-6. Note
that font size formating is skiped here. Default=2.
wTxt: Writes a formated string of text.
wList: Writes a list based on a given array. The natural is a string array,
but it forces the to_s of the object so if you have these implemented, it will
work.
You can pass two extra parameters:
type: 'dd', 'ul', 'ol' for each of the posible html lists. Default is ul.
style: 'square' or 'i'(and others) for each of the posible graphics for the lists. Default are
1, circle.
wBr: writes a single <BR>. You can also set and extra numeric argument so
it writes all that BR's.
wHr: Like the Wbr, but with <hr>
wTable: This method can write a table showing the values of:
-RGSS table class
-A array that contents other arrays with simplier numerics or strings
-A array of equals structs or hashes
-A array of equals arrays, setting the rows tags apart
-A array of equals objects, while they have a valid to_a method
Note that method can define the most important configurations that html suport,
like cellspacing, widht, etc.
wP(text, align): You can write a new paragraph with setting the alignement
for it. The default align is left.
wLink(link, description): Writes a html link with optional description text.
If not passed, description is the link dir.
wImg(img, width, height): Writes a image, and also you can set optionally
the width and height. Default values are 100
STARTING METHODS
wHead(title): This method writes all the header. You have to pass
a optional string for the title of the page. The default is ''
wBody(bgcolor): This starts the body, fixing the bgcolor. If not bgcolor
is passed, it will use the atribute defined one.
Note that is better to not use this methods and send this options in the
constructor.
ENDING AND SAVING METHODS
EndSave: Use this method if you just want to end a html document and save
it directly to the file. No other uses for it.
wEnd: This writes the ending of html, but it dont save it.
Save: This save the document to the specified file.
NON FORMATED TEXT AND EXTRA CODE
If you want to show a non formated text, or a add a completely yours thing
to the code, or make any other special thing, you have to use the write code
method(wCode)
This dont add any extra code to the string you pass, so its a clean way of adding
some things to the contents.
ADDING SPECIAL FORMATINGS
The way of adding extra formating is calling wTxt sending the text with
the special html tags. Example:
doc.wTxt [sub] example text [/sub]
EXAMPLE
doc = HtmlDoc.new 'log', 'Title: You can set title through constructor also',
'black'
doc.b=true # Set the formating b to true
doc.wH 'Combat against KL', 1 #Writes a html h1
doc.b=false
doc.Form '20' # Changues the font size to 20
doc.wLine 'Testing line' # Adds a <br> at the end
doc.wList ['wep', 'wea', 'yu'] # Show a htl list with that entries
doc.SaveEnd # End html doc and save into the html file