Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Creating HTML from SMCL log file including graphics - revised log2html.ado


From   "David Elliott" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Creating HTML from SMCL log file including graphics - revised log2html.ado
Date   Fri, 19 May 2006 07:28:24 -0300

Good observations & questions that I will try to address in turn:

1) The restriction to only png is that, if this is going to be
rendered as HTML, this is the only bitmap file format that can be
displayed on all the browsers I tested.

2) If you examine the output from the following 4 -graph export-
commands you will see that the extension is not included when you use
the [,as(png)] argument:

. twoway mband weight mpg

. graph export test, as(png)
(file test written in PNG format)

. graph export test.png, replace
(file test.png written in PNG format)

. graph export c:\web\test, as(png) replace
(file c:\web\test written in PNG format)

. graph export c:\web\test.png, replace
(file c:\web\test.png written in PNG format)

thus, we have to be able to capture the different output, with and
without the .png extension

3) Ideally, one would have paths relative to the directory in which
the html log was written.  Unfortunately STATA's string handling
functions don't include drive|path|filename|extension aware functions
like JUSTDRIVE( ) | JUSTEXT( ) | JUSTFNAME( ) | JUSTSTEM( ) that would
allow parsing the various file locations and their relationships
relative to each other.  For ease of coding and possibility of moving
the htmllog and images to a different directory in the future, I
believe they should be in the same directory.  If you hard code the
directories, moving the files will break the links.  For Internet
Explorer, a reference to src="c:\web\test.png" will work, but in
Gecko-based browsers like Netscape|Firefox, it will fail.

4) I am aware on the inability to include multiple word filenames.  As
a general rule, it is inadvisable to have space-separated filenames in
web addresses.  I could pick up the entire filename, or parse back to
the preceding '\' or '/' but it is harder than it looks.  I am unable
to find the index(...) function to which you refer although there is
an indexnot(...) function referenced under -help string_functions-

5) Any suggestions as to what verbatim options you would want,
remembering that it would be applied to all images?  The <img> tag
doesn't have a lot of attributes.  While good coding practice would
have the height and width explicitly specified, this only really helps
when rendering complex designs since the layout engine is able to know
the dimensions of images before they have fully downloaded.  You have
the dimensions only if explicitly specified with the height(600)
width(800) arguments.

Thank you so much for your interest and I look forward to further
suggestions from you and others.


On 5/19/06, David Harrison <[email protected]> wrote:
A few comments and suggestions on -grlog2html-.

1. It wouldn't take much to relax the restriction on only doing PNG
files... I think "file .* written in .* format" would do it (unless
there are any commands other than -graph export- that give this
message..., maybe there are, I can't think right now).

2. I believe Stata always includes the file extension in its message if
there is one, so the code to add .png if it is missing will not do
anything helpful.

3. Why do you remove the path, when there is one? If I put the image
into a different folder, then I would want the html to point there.

4. The code will fail for multiple word file names, e.g. if I export "my
graph.png", it will add a link to "my.png". For paths with spaces in,
you will end up with the last word before the space followed by .png. My
best 5 minute go at sorting this would be to code:

local gr_name = substr(`"`line'"', 7, index(`"`line'"',"written in ")-7)

This should only fail if the file name (or path) contains "written in "
(or is ridiculously long).

5. One final suggestion- It would be quite straight forward to add an
extra option to be put verbatim into the <img> tag.

David

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of David Elliott
Sent: 19 May 2006 02:12
To: [email protected]
Subject: Re: st: RE: Creating HTML from SMCL log file including graphics
- revised log2html.ado


X--- revised grlog2html.ado  code follows ---X

*! grlog2html 1.2.8  cfb/njc/br/dce 08may2006 *! log2html 1.2.8
cfb/njc/br 09feb2005 *! log2html 1.2.7  cfb/njc/br 12oct2004 *! log2html
1.2.6  cfb/njc/br 2oct2003 *! log2html 1.2.5  cfb/njc/br 17Jun2003 *!
log2html 1.2.0  cfb/njc  3Mar2003 *! log2html 1.1.1  cfb/njc  17Dec2001
program grlog2html
   version 8.0
   syntax anything(name=smclfile id="The name of a .smcl logfile is")
///
   [, ERASE replace TItle(str) INput(str) Result(str) BG(str)  ///
        LINEsize(numlist max=1 int >=40 <=255) ///
   TExt(str) ERRor(str) PERcentsize(integer 100) BOLD CSS(str) ///
        SCHeme(str) GRaphs]
// ###########Note addition of a GRaphs switch###########
   // syntax processing

   if "`css'" != "" & `"`input'`result'`text'`error'`bg'`scheme'"' != ""
{
       di as err "if CSS is specified, you may not specify any colors"
       exit 198
   }

   if "`scheme'" != "" {
       if `"`input'`result'`text'`error'`bg'"' != "" {
           di as err ///
       "if a scheme is specified, you may not specify any colors"
           exit 198
       }

       local names   "bg     input  result text   error"
       local cblack  "000000 ffffff ffff00 00ff00 ff0000"
       local cwhite  "ffffff 000000 000000 000000 ff0000"
       local cblue   "000090 ffffff ffff00 00ff00 ff0000"
       local cugly   "ff00ff 9999ff ff99ff 00ff00 cccc00"
       local cyellow "ffffcc cc00cc 0000cc 000000 ff0000"

       local cnt 1
       foreach name of local names {
           local `name' : word `cnt++' of `c`scheme''
       }
       if "`bg'"=="" {
           display as error "scheme `scheme' does not exist!
Available schemes are "
           display as error "  black, white, blue, and yellow."
           exit 198
       }

    }

   else {
       if "`input'" == ""  local input "CC6600"
       if "`result'" == "" local result "000099"
       if "`text'" == ""   local text "000000"
       if "`error'" == ""  local error "dd0000"
       if "`bg'" == ""     local bg "ffffff"
       else {
           if "`bg'" == "gray" | "`bg'" == "grey" {
               local bg "cccccc"
           }
       }
   }

   if `percentsize' <= 0 {
       display as error "percentsize() must be an integer larger than
zero"
       exit 198
   }

   // filenames and handles

   tempname hi ho
   tempfile htmlfile
   local origfile `smclfile'
   if (!index(lower("`origfile'"),".smcl")) {
       local origfile  "`origfile'.smcl"
       }
   local smclfile : subinstr local smclfile ".smcl" ""
   local smclfile : subinstr local smclfile ".SMCL" ""
   local smclfile : subinstr local smclfile `"""' "", all /* '"' (for
fooling emacs) */
   local smclfile : subinstr local smclfile "`" "", all
   local smclfile : subinstr local smclfile "'" "", all
   local outfile `"`smclfile'.html"'
   if "`linesize'" != "" local ll "ll(`linesize')"
   qui log html `"`origfile'"' `"`htmlfile'"', `replace' yebf whbf `ll'

   // line-by-line processing

   file open `hi' using `"`htmlfile'"', r
   file open `ho' using `"`outfile'"', w `replace'
   file write `ho'  _n
   file write `ho' "<html>" _n "<head>" _n
   if `"`title'"' ~= "" {
       file write `ho' `"<title>`title'</title>"' _n
   }
   file write `ho' `"<meta http-equiv="Content-type"
content="text/html; charset=iso-8859-1">"' _newline
   file write `ho' `"<meta http-equiv="Content-Style-Type"
content="text/css">"' _newline
   if "`css'" == "" {
       file write `ho' `"<style type="text/css">"' _newline
       file write `ho' "BODY{background-color: `bg';" _newline
       file write `ho' `"    font-family: monaco, "courier new",
monospace;"' _newline
       if `percentsize' != 100 {
           file write `ho' "font-size:`percentsize'%;" _newline
       }
       file write `ho' "     color: #`text'}" _newline
       if "`bold'" != "" {
           file write `ho' ".input, .result, .error{font-weight:
bold}" _newline
       }
       file write `ho' ".input {color: #`input'}" _newline
       file write `ho' ".result{color: #`result'}" _newline
       file write `ho' ".error{color: #`error'}" _newline
       file write `ho' "</style>" _newline
   }
   else {
       file write `ho' `"<link rel="stylesheet" href="`css'">"' _newline
   }
   file write `ho' "</head>" _newline
   file write `ho' "<body>" _newline
   if `"`title'"' != "" {
       file write `ho' `"<h2>`title'</h2>"' _n
   }

   file read `hi' line

   local cprev = 0

   while r(eof) == 0 {

       // change <p> (which should be a div) to a <br><br>
       local line : ///
   subinstr local line "<p>" "<br><br>", all

       local line: ///
   subinstr local line "<b>. " "<span class=input>. ", count(local c)

       // catch continuation lines
       local word1 : word 1 of `"`line'"'
       if substr(`"`word1'"',1,7) == "<b>&gt;" & `cprev' {
           local line : ///
   subinstr local line "<b>" "<span class=input>", count(local c)
           }
       else {
           local line: ///
   subinstr local line "<b>" "<span class=result>", all
           }

       local line: subinstr local line "</b>" "</span>", all

       // check for error number
       if substr(`"`word1'"',1,2) == "r(" {
           if real(substr(`"`word1'"',3,index(`"`word1'"',")")-3)) < . {
                local line `"<div class=error> `line' </div>"'
           }
       }

// ###########Graphic inclusion routine starts here###########
   if "`graphs'" != "" {
       if regexm(`"`line'"',"file.*written in PNG format") { //check if
png was written
           local gr_name : word 2 of `line'
           if regexm(`"`gr_name'"',"(.*\\.*)") {  //backslash
               local gr_name = ///
               word(subinstr(`"`gr_name'"',"\"," ",.),-1)
               }
           if regexm(`"`gr_name'"',"(.*/.*)") {  //forward slash
               local gr_name = ///
               word(subinstr(`"`gr_name'"',"/"," ",.),-1)
               }
           if regexm(`"`gr_name'"',".*png")==0 { //add .png if missing
               local gr_name = `"`gr_name'.png"'
           }
           local line "<img src="`gr_name'" alt="`gr_name'" />"
      }
}
// ###########and finishes here###########

       file write `ho' `"`macval(line)'"' _n
       local cprev = `c'
       file read `hi' line
   }

   file write `ho' "</body>" _n "</html>" _n
    file close `ho'

   di _n `"HTML log file `outfile' created"'
   if "`erase'" ~= "" {
       erase `"`origfile'"'
       }
end

X--- code ends ---X



______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


--
David Elliott

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index