Question

I have a piece of Stata code that I want to post on my website.

The code looks like this:

/* creating temporary files*/
forval i = 1964/2012 {
use mar`i', clear
unab allvars: _all
local masterlist "income race age grdatn edlev grdhi hisp spneth recnum state cntyfp cbsa hhtype hhid numper"
local keeplist: list allvars & masterlist
keep `keeplist'
save temporaryMar`i'
}
capture erase stateLevel`yearStart'-`yearEnd'.dta
/* end of temporary files establishment */


/*  *** INCOME ADJUSTMENTS ***
**  variable name "incomeStandard". making sure that 
**  income does not contain any coded values. ................ etc

In order to transform it into a good looking HTML and post it on my website I need to write the following:

<div class="geshifilter">
<pre class="java geshifilter-java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Integers <span style="color: #009900;">{</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">(</span><span style="color: #003399;">String</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> arguments<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//declaring a variable</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* Using for loop to repeat instruction execution */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">(</span>c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">(</span>c<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>
  <span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre></div>

There is too much hassle doing this. Is there any easier way? like a syntax highlighter?

Was it helpful?

Solution

There is at least one highlighter in your sense for Stata, log2html, to be installed from SSC. Given a do.file, you could do this

 . ssc inst log2html 
 . log using mydo.smcl
 . type mydo.do
 . log close 
 . log2html mydo.smcl 

and you have an HTML file. You need to edit out some lines and the style choices may not suit. Systematic variations would be a matter for a decent text editor or scripting language. The installation using ssc need only be done once.

Incidentally, for other readers if not yourself, the line

 . capture erase stateLevel`yearStart'-`yearEnd'.dta

would make no sense without prior definition of the local macros used within the same .do file.

OTHER TIPS

Yes there is and I wrote it! :-)

It's a JavaScript engine called Statax and you can easily implement it on any website or blog, simply by adding a single line of code and putting the Stata code in a tag with sh.stata class.

You can read the details on my website: http://www.haghish.com/statax/statax.php

This is how it goes:

  • In the heading of the web-page add the following script <script type="text/javascript" src='http://haghish.com/statax/Statax.js'></script>
  • That's all!

Now all you need is to place the Stata code in a pre tag. But, you also should specify the class i.e., <pre class="sh_stata">. That's all. Any Stata code you place from now till the </pre> tag, will be highlighted just as good as Stata do-file editor. You'll find more examples on my website and also can learn how to customize it and use any of the syntax highlighting themes, which are all made by applying Stata themes.

I also wrote a package for it, called Statax which is available on ssc ssc install statax. It basically uses the same JavaScript engine. However, it allows you to convert your .do and .ado files to HTML and apply the syntax highlighter automatically, just to make life easier...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top