Statalist The Stata Listserver


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

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


From   "David Elliott" <[email protected]>
To   [email protected]
Subject   st: Creating HTML from SMCL log file including graphics - revised log2html.ado
Date   Thu, 18 May 2006 19:19:15 -0300

*This may appear as a double post to some but my original post did not
seem to make it to the http://www.stata.com/statalist/archive/2006-05/
archive so it may be lost in cyberspace*

Revised log2html.ado (BETA)

Please find below a revision of a routine that takes a smcl log file
and converts it to HTML.  I have added a component that will add <img>
tags to the HTML output to display png graphs that have been exported.

Requirements:

(1) Graphs must have been successfully exported with -graph export
*.png- or -graph export *, as(png)-
(2) The png graphs, logfile, and html file must all be in the same directory
(3) Graphs are not included by default and must be specified using the
graphs option which can be abbreviated to gr

Notes:
(1) If the HTML file is moved, all referenced graphs must be moved as
well or else the links will break.
(2) The routine *should* work for *nix style paths with forward
slashes but has not been tested in that environment.
(3) If someone has a better way to pop a filename off the end of a
path, or a way to implement relative addressing to graphics files in
other than the logfile directory, please offer suggestions.

X--- revised log2html.ado  code follows ---X

*! log2html 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 log2html
   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
Remember to have the extra carriage return after the end statement or
you will get an unexpected end of file error ;-)

DCE
--
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