/******************************************************************************* Developed by E. F. Haghish (2014) Center for Medical Biometry and Medical Informatics University of Freiburg, Germany haghish@imbi.uni-freiburg.de * Synlight package comes with no warranty * Synlight version 1.0 August, 2014 Synlight version 1.1 September, 2014 */ program synlight version 11 syntax anything(name=smclfile id="The smclfile name is") /// [, erase replace CFont(str) Font(str) Title(str) Size(numlist) /// STYle(name) css(str)] *read the Global Macro including the word lists synlightlist ******************************************************************** *SYNTAX PROCESSING ******************************************************************** local input `smclfile' if (!index(lower("`input'"),".smcl")) { local html "`input'.html" local tempfile "`input'_temp.html" local corrected "`input'_temp.smcl" local input "`input'.smcl" } /* DEFINING THE FONT */ if "`cfont'" == "" { local cfont Menlo-Regular, monaco, Courier New; } if "`font'" == "" { local font Courier New; } if "`size'" == "" { local size 12 } if "`css'" == "" { /* STATA STYLES */ if "`style'" == "" | "`style'" == "stata" | "`style'" == "Stata" /// | "`style'" == "st" { *> colors local command .command{color:#00008A;font-size:`size'px;font-family:`cfont'} local function .function{color:#00F;font-family:`cfont'} local macro .macro{color:#008080;font-family:`cfont'} local string .string{color:#800000;font-family:`cfont'} local overstring color:#800000; local digit .digit{color:#0052FF;font-family:`cfont'} local comment .comment{color:#0F7F11;font-family:`cfont'} local overcomment color:#0F7F11; local brace .brace{color:#FF2600;font-family:`cfont'} local sign .sign{color:#000;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; } local header header{font-size:28px; text-align:center; padding-bottom:28px;} local code .code{background-color:#EBEBEB;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font'} local p p{white-space:normal; font-family:`font'} } /* DARING STYLES */ if "`style'" == "daring" | "`style'" == "dar" { *> colors local command .command{color:#5E97F4;font-size:`size'px;font-family:`cfont'} local function .function{color:#96A6C8;font-family:`cfont'} local macro .macro{color:#95A99F;font-family:`cfont'} local string .string{color:#73C935;font-family:`cfont'} local overstring color:#73C935; local digit .digit{color:#FFDC33;font-family:`cfont'} local comment .comment{color:#CC8C3C;font-family:`cfont'} local overcomment color:#CC8C3C; local brace .brace{color:#906;font-family:`cfont'} local sign .sign{color:#FFB052;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#333333;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; color: white; border-radius:7px;} local output .output{color:#F4F4FF; display:block; white-space:pre; font-size:`size'px;font-family:`font'} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; background-color:#282828;} local header header{font-size:28px; text-align:center; padding-bottom:28px;color:white;} local p p{white-space:normal; font-family:`font'; color:#F4F4FF;} } /* MIDNIGHT STYLE*/ if "`style'" == "midnight" | "`style'" == "mid" { *> colors local command .command{color:#D31795;font-size:`size'px;font-family:`cfont'} local function .function{color:#FEFFFE;font-family:`cfont'} local macro .macro{color:#E47C48;font-family:`cfont'} local string .string{color:#FF2C38;font-family:`cfont'} local overstring color:#FF2C38; local digit .digit{color:#786DFF;font-family:`cfont'} local comment .comment{color:#41CC45;font-family:`cfont'} local overcomment color:#41CC45; local brace .brace{color:#FEFFFE;font-family:`cfont'} local sign .sign{color:#4971FA;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#564F30;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; color:white;} local output .output{color:#FEFFFE; display:block; white-space:pre; font-size:`size'px;font-family:`font'} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; background-color:black;} local header header{font-size:28px; text-align:center; padding-bottom:28px;color:#837C60;} local p p{white-space:normal; font-family:`font'; color:#FEFFFE;} } /* SUNSET STYLE */ if "`style'" == "sunset" | "`style'" == "sun" { *> colors local command .command{color:#294277;font-size:`size'px;font-family:`cfont'} local function .function{color:#4E7CBD;font-family:`cfont'} local macro .macro{color:#646485;font-family:`cfont'} local string .string{color:#DF0707;font-family:`cfont'} local overstring color:#DF0707; local digit .digit{color:#294277;font-family:`cfont'} local comment .comment{color:#C3741C;font-family:`cfont'} local overcomment color:#C3741C; local brace .brace{color:#CD5C5C;font-family:`cfont'} local sign .sign{color:#4268CC;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#F9DF9C;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; border:0px solid; border-radius: 10px;} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font'} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%;background-color:#FFFCE5;} local header header{font-size:28px; text-align:center; padding-bottom:28px;} local p p{white-space:normal; font-family:`font';} } /* IMBI STYLE */ if "`style'" == "IMBI" | "`style'" == "imbi" { *> colors local command .command{color:#00008A;font-size:`size'px;font-family:`cfont'} local function .function{color:#9A88C6;font-family:`cfont'} local macro .macro{color:#718C00;font-family:`cfont'} local string .string{color:#F66;font-family:`cfont'} local overstring color:#F66; local digit .digit{color:#F1A517;font-family:`cfont'} local comment .comment{color:#0F7F11;font-family:`cfont'} local overcomment color:#0F7F11; local brace .brace{color:#49AABE;font-family:`cfont'} local sign .sign{color:#FF7312;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#F5F5F5;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; border:0px solid; border-radius:10px; -moz-box-shadow: 1px 2px 3px #d4d4d4;-webkit-box-shadow: 1px 1px 3px #d4d4d4;box-shadow: 1px 1px 3px #d4d4d4; color:#585858;} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`cfont';color:#6D6D6D;} local general body, code, div, span {font-family:`cfont'} local body body{padding:20px 3% 0 3%; background-color: #FBFBFB;} local header header{font-size:28px; text-align:center; padding-bottom:28px;} local p p{white-space:normal; font-family:`cfont';} } /* COBALT STYLE */ if "`style'" == "cobalt" | "`style'" == "cob" { *> colors local command .command{color:#EA9B27;font-size:`size'px;font-family:`cfont'} local function .function{color:#FF8000;font-family:`cfont'} local macro .macro{color:#FF378E;font-family:`cfont'} local string .string{color:#FF6;font-family:`cfont'} local overstring color:#FF6; local digit .digit{color:#66F;font-family:`cfont'} local comment .comment{color:#0088FB;font-family:`cfont'} local overcomment color:#0088FB; local brace .brace{color:#FF2600;font-family:`cfont'} local sign .sign{color:#0080FF;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#062851;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; color:white;} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font';color:#FBFBFB;} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; background-color: #002240;} local header header{font-size:28px; text-align:center; padding-bottom:28px;color:#FF0080;} local p p{white-space:normal; font-family:`font';} } /* BLACKFOREST STYLE */ if "`style'" == "blackforest" | "`style'" == "bl" { *> colors local command .command{color:#B4881D;font-size:`size'px;font-family:`cfont'} local function .function{color:#85981C;font-family:`cfont'} local macro .macro{color:#2C9E97;font-family:`cfont'} local string .string{color:#FD97C8;font-family:`cfont'} local overstring color:#FD97C8; local digit .digit{color:#2D6092;font-family:`cfont'} local comment .comment{color:#5C7782;font-family:`cfont'} local overcomment color:#5C7782; local brace .brace{color:#49AABE;font-family:`cfont'} local sign .sign{color:#FF7312;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#063642;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; color:#8EA1A1;} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font';color:#6D6D6D;} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; background-color: #002B35;} local header header{font-size:28px; text-align:center; padding-bottom:28px;color:#5C7782;} local p p{white-space:normal; font-family:`font';} } /* DESERT STYLE */ if "`style'" == "desert" | "`style'" == "des" { *> colors local command .command{color:#C5783B;font-size:`size'px;font-family:`cfont'} local function .function{color:#85981C;font-family:`cfont'} local macro .macro{color:#2C9E97;font-family:`cfont'} local string .string{color:#C2BB5F;font-family:`cfont'} local overstring color:#C2BB5F; local digit .digit{color:#FFEF6F;font-family:`cfont'} local comment .comment{color:#B5925C;font-family:`cfont'} local overcomment color:#B5925C; local brace .brace{color:#AC0202;font-family:`cfont'} local sign .sign{color:#FF7312;font-family:`cfont'} local error .error{color:#F00;font-family:`cfont'} *> body local code .code{background-color:#363636;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'; color:#EBEBEB;} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font';color:#C8BCA3;} local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; background-color: #303030;} local header header{font-size:28px; text-align:center; padding-bottom:28px;color:#B5925C;} local p p{white-space:normal; font-family:`font';} } } if "`css'" ~= "" { *> body local general body, code, div, span {font-family:`font'} local body body{padding:20px 3% 0 3%; } local header header{font-size:28px; text-align:center; padding-bottom:28px;} local code .code{background-color:#EBEBEB;display:block; /// padding:10px; font-size:`size'px; font-family:`cfont'} local output .output{display:block; white-space:pre; font-size:`size'px;font-family:`font'} local p p{white-space:normal; font-family:`font'} } if "`style'" ~= "cobalt" & "`style'" ~= "cob" & /// "`style'" ~= "imbi" & /// "`style'" ~= "blackforest" & "`style'" ~= "bl" & /// "`style'" ~= "desert" & "`style'" ~= "des" & /// "`style'" ~= "sunset" & "`style'" ~= "sun" & /// "`style'" ~= "midnight" & "`style'" ~= "mid" & /// "`style'" ~= "daring" & "`style'" ~= "dar" & /// "`style'" ~= "" & "`style'" ~= "stata" & "`style'" ~= "Stata" & /// "`style'" ~= "st" { di as error "{p}{bf:`style'} style was not found. The available styles are {bf:stata}, {bf:daring}, {bf: midnught}, {bf:sunset}, {bf:imbi}, or {bf:cobalt} is expected{smcl}" exit 198 } ******************************************************************** *CORRECTING THE SMCL LOG FILE ******************************************************************** qui copy `input' `corrected', replace tempname hitch canvas file open `hitch' using `"`input'"', read file open `canvas' using `"`corrected'"', write replace file read `hitch' line while r(eof) == 0 { local word1 : word 1 of `"`macval(line)'"' *>removing indents foreach i of numlist 64/0 { local indent : di _dup(`i') " " local b = `i'+1 *removing the indents after ">" if substr(`"`macval(word1)'"',1,2) == "> " { local indent : di _dup(`b') " " local line : subinstr local line /// `">`indent'"' ">", all } *Indents after after "." if substr(`"`macval(word1)'"',1,`b') == ".`indent'" /// & `"`line'"' >= ".`indent'" { local line : subinstr local line /// `".`indent'"' ".", all } *Indents after after "{com}." local b = `i'+7 if substr(`"`macval(word1)'"',1,`b') == "{com}. `indent'" { cap local line : subinstr local line /// "{com}. `indent'" "{com}. ", all } } *> replacing the "dots" with "{com}. " if substr(`"`macval(word1)'"',1,1) == "." & `"`line'"' > "." { local h : di substr(`"`macval(line)'"',2,.) local line `"{com}. `macval(h)'"' } file write `canvas' `"`macval(line)'"' _n file read `hitch' line } file close `canvas' file close `hitch' ******************************************************************** *TRANSLATING SMCL to HTML ******************************************************************** qui log html `corrected' `html', `replace' if "`erase'" == "erase" { cap qui erase `input' } cap qui erase `corrected' ******************************************************************** *PROCESSING THE HTML FILE ******************************************************************** *APPENDING LONG LINES qui copy `html' `tempfile', replace tempname hitch canvas file open `hitch' using `"`tempfile'"', read file open `canvas' using `"`html'"', write replace file read `hitch' line while r(eof) == 0 { local word1 : word 1 of `"`line'"' local line : subinstr local line "
" "", all local line : subinstr local line "" "", all **************************************************************** *REMOVING THE LINES THAT ONLY HAVE DOTS **************************************************************** *REMOVING LINES THAT ARE ONLY A DOT if `"`line'"' == "." { local line : subinstr local line "." " " } if `"`line'"' == ". " { local line : subinstr local line ". " " " } if substr(`"`word1'"',1,4) == ">" { local line : subinstr local line `">"' `"
" | `"`line'"' == "." {
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
while substr(`"`word1'"',1,2) == " " & ///
substr(`"`word1'"',1,3) ~= " " {
local host `"`macval(host)'"'`"
"'`"`macval(line)'"'
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
*if there are more than 9 lines...
if substr(`"`word1'"',1,4) == " 10." {
while substr(`"`word1'"',1,1) == " " & ///
substr(`"`word1'"',1,2) ~= " " {
local host `"`macval(host)'"'`"
"'`"`macval(line)'"'
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
}
file write `canvas' `"`macval(host)'"' _n
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
}
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file close `canvas'
file close `hitch'
********************************************************************
*REPLACING QUOTES
********************************************************************
qui copy `html' `tempfile', replace
tempname hitch canvas
file open `hitch' using `"`tempfile'"', read
file open `canvas' using `"`html'"', write replace
file read `hitch' line
while r(eof) == 0 {
local word1 : word 1 of `"`line'"'
if substr(`"`word1'"',1,2) == ". " {
local line : subinstr local line ///
`"""' `"""', all
}
/* FIXING THE COMMENTS */
if substr(`"`word1'"',1,4) == ". /*" {
local line : subinstr local line ///
`". /*"' `"
/*"', all } local line : subinstr local line `"*/"' `"*/
"', all *REMOVE QUI LOG C local line : subinstr local line ". qui log c" "", all file write `canvas' `"`macval(line)'"' _n file read `hitch' line } file close `canvas' file close `hitch' *PART 5: HANDLING THE COMMENTS qui copy `html' `tempfile', replace tempname hitch canvas file open `hitch' using `"`tempfile'"', read file open `canvas' using `"`html'"', write replace file read `hitch' line while r(eof) == 0 { local word1 : word 1 of `"`line'"' *DEFINING "///" "//" "{" "}" " if substr(`"`word1'"',1,2) == ". " & `"`line'"' > ". " { local line : subinstr local line /// "///" `"///"'`"`macval(line)'"'`"
"' _n
file write `canvas' `"" { local line : subinstr local line "
" "", all } file write `canvas' `"`macval(line)'"' _n file read `hitch' line } file close `canvas' file close `hitch' *REMOVING EMPTY LINES qui copy `html' `tempfile', replace tempname hitch canvas file open `hitch' using `"`tempfile'"', read file open `canvas' using `"`html'"', write replace file read `hitch' line while r(eof) == 0 { local word1 : word 1 of `"`line'"' *JUMP OVER EMPTY LINE if `"`line'"' == "" { local host `"`line'"' file read `hitch' line local word1 : word 1 of `"`line'"' if `"`line'"' == "" { file write `canvas' `"`macval(line)'"' _n file read `hitch' line local word1 : word 1 of `"`line'"' } } if substr(`"`word1'"',1,20) == `"
"' {
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
*JUMP OVER EMPTY LINE
if `"`line'"' == "" {
file read `hitch' line
local word1 : word 1 of `"`line'"'
}
if substr(`"`word1'"',1,19) ~= `""' & `"`line'"' ~= "" {
file write `canvas' `"`macval(host)'"' _n
}
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file close `canvas'
file close `hitch'
********************************************************************
*STRING (QUOTE) SYNTAX HIGHLIGHT
********************************************************************
qui copy `html' `tempfile', replace
tempname hitch canvas
file open `hitch' using `"`tempfile'"', read
file open `canvas' using `"`html'"', write replace
file read `hitch' line
while r(eof) == 0 {
local word1 : word 1 of `"`line'"'
if substr(`"`macval(word1)'"',1,19) == `""' {
forval i = 1/20 {
local line = subinstr(`"`macval(line)'"', ///
""",`"""',1)
local line = subinstr(`"`macval(line)'"', ///
""",`"""',1)
}
}
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file close `canvas'
file close `hitch'
********************************************************************
*GLOBAL MACRO SYNTAX HIGHLIGHT
********************************************************************
qui copy `html' `tempfile', replace
tempname hitch canvas
file open `hitch' using `"`tempfile'"', read
file open `canvas' using `"`html'"', write replace
file read `hitch' line
while r(eof) == 0 {
local word1 : word 1 of `"`line'"'
if substr(`"`word1'"',1,19) == `""' {
local line : subinstr local line "$" "$", all
local b `"`macval(line)'"'
foreach word of local line {
if substr(`"`word'"', 1, 5) == "$" {
local b : di subinstr(`"`macval(b)'"',`"`macval(word)'"', ///
`"`macval(word)'"',1)
}
}
local line `"`macval(b)'"'
}
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file close `canvas'
file close `hitch'
********************************************************************
*CREATING THE HTML FILE
********************************************************************
qui copy `html' `tempfile', replace
tempname hitch canvas
file open `hitch' using `"`tempfile'"', read
file open `canvas' using `"`html'"', write replace
file write `canvas' `""' _newline
file write `canvas' `""' _newline
file write `canvas' `""' _newline
file write `canvas' `""' _newline ///
`""' _n ///
`""' _n ///
`""' _n
file write `canvas' `"`title' "' _newline
file write `canvas' `"" _n(4)
if "`css'" ~= "" {
file write `canvas' `"" _newline
file write `canvas' "" _newline
file write `canvas' `"`title' "' _newline
file read `hitch' line
while r(eof) == 0 {
local word1 : word 1 of `"`line'"'
file write `canvas' `"`macval(line)'"' _n
file read `hitch' line
}
file write `canvas' "" _newline
file write `canvas' "" _newline
file close `canvas'
file close `hitch'
cap qui erase `tempfile'
di _n(4)
di as txt " (~|_|._ | o(~||_ _|_"
di as txt " _) _|| ||_| _|| | | created "`"{bf:{browse "`html'"}} "' _n(2)"
* drop wordlist macros
macro drop synlightlist
macro drop synfunclist
*check for synlight updates
synlightversion
end