*! version 0.8.4 09nov2026 Eric Booth *! _sm_renderflow -- draw a paths journal (pnode/pflow/ppath rows) as a *! self-contained HTML page: one column per item, one block per common *! answer, ribbons between consecutive columns sized by the people who gave *! both answers, and a table of the most common full sequences underneath. *! *! Everything drawn is read back from the journal; nothing is recounted. *! The one derived quantity is geometry: block heights share a single *! people-to-pixels scale, so a block twice as tall is twice as many *! people, in every column. *! *! Escaping discipline is _sm_renderhtml's: journal text is *! neutralized to marker characters at import, carried through macros *! inertly, and mapped to XML entities at the moment of writing. program define _sm_renderflow, sclass version 16 syntax using/, SAVing(string) [TITLe(string) NAME(string) /// HIGHlight(string) EMBed replace] sreturn clear local hf `"`saving'"' if strlower(substr(`"`hf'"', -5, .)) != ".html" local hf `"`hf'.html"' capture confirm file `"`hf'"' if !_rc & "`replace'" == "" { di as err `"surveymap paths: `hf' exists; add replace to overwrite"' exit 602 } * ---- intake ---------------------------------------------------------- tempname J frame create `J' frame `J' { quietly import delimited using `"`using'"', delimiter(tab) /// varnames(1) stringcols(_all) encoding("utf-8") bindquote(nobind) clear capture confirm variable class local badc = _rc capture confirm variable gatevar if `badc' | _rc { display as error "`using' is not a surveymap journal (no " /// "class/gatevar column; see proto/JOURNAL_SCHEMA.md in the GitHub repository)" exit 459 } foreach v in seq class var position vallabel value gatevar n_asked /// n_answered n_nonresp n_sysmiss pct_answered rate status gate /// gated_by pooled type severity flags { capture confirm variable `v' if _rc quietly generate str1 `v' = "." quietly replace `v' = "." if `v' == "" } foreach v in class var vallabel value gatevar status gate gated_by /// pooled type severity flags { quietly replace `v' = subinstr(`v', "&", char(16), .) quietly replace `v' = subinstr(`v', "<", char(17), .) quietly replace `v' = subinstr(`v', ">", char(18), .) quietly replace `v' = subinstr(`v', char(96), char(6), .) quietly replace `v' = subinstr(`v', char(36), char(7), .) quietly replace `v' = subinstr(`v', char(34), char(19), .) } } frame `J': local NR = _N * ---- survey row: N, the settings line, the scope --------------------- local NN "." local sflags "" local K = 0 forvalues i = 1/`NR' { frame `J': local cls = class[`i'] if "`cls'" == "survey" { frame `J': local NN = n_asked[`i'] frame `J': local sflags = flags[`i'] } if "`cls'" != "item" continue frame `J': local p = real(position[`i']) if `p' >= . continue local K = max(`K', `p') frame `J': local v_`p' = var[`i'] frame `J': local lb_`p' = vallabel[`i'] } if `K' < 2 { frame drop `J' di as err "surveymap paths: `using' has no item rows to draw" di as err " write the journal with surveymap paths varlist, out(...)" exit 459 } local NSEQ "" if ustrregexm(`"`sflags'"', "([0-9]+) distinct full sequences") { local NSEQ = ustrregexs(1) } local NUNI "" if ustrregexm(`"`sflags'"', "([0-9]+) interviews on unique routes") { local NUNI = ustrregexs(1) } local scopetxt "" local p = strpos(`"`sflags'"', "; scope: ") if `p' { local scopetxt = strtrim(substr(`"`sflags'"', `p' + 9, .)) } * ---- blocks (pnode), ribbons (pflow), sequences (ppath) -------------- local KM = 0 forvalues i = 1/`NR' { frame `J': local cls = class[`i'] if "`cls'" == "pnode" { frame `J': local t = real(position[`i']) frame `J': local s = real(gate[`i']) if `t' >= . | `s' >= . continue local KM = max(`KM', `s') frame `J': local nk_`t'_`s' = real(n_asked[`i']) if `nk_`t'_`s'' >= . { frame drop `J' di as err "surveymap paths: a pnode row in `using' has no count" di as err " the journal is damaged; rewrite it with surveymap paths ..., nodraw" exit 459 } frame `J': local pk_`t'_`s' = pct_answered[`i'] frame `J': local dk_`t'_`s' = vallabel[`i'] frame `J': local vk_`t'_`s' = value[`i'] } if "`cls'" == "pflow" { frame `J': local t = real(position[`i']) frame `J': local a = real(gate[`i']) frame `J': local b = real(rate[`i']) if `t' >= . | `a' >= . | `b' >= . continue local FL_`t' "`FL_`t'' `a':`b'" frame `J': local fn_`t'_`a'_`b' = real(n_asked[`i']) frame `J': local fp_`t'_`a'_`b' = pct_answered[`i'] } if "`cls'" == "ppath" { frame `J': local r = real(gate[`i']) if `r' >= . continue if "`NPATH'" == "" local NPATH = 0 local NPATH = max(`NPATH', `r') frame `J': local sq_`r' = value[`i'] frame `J': local sn_`r' = n_asked[`i'] frame `J': local sp_`r' = pct_answered[`i'] } } if `KM' == 0 { frame drop `J' di as err "surveymap paths: `using' has no pnode rows" di as err " it is a scan journal; draw it with surveymap draw" exit 459 } frame drop `J' * top comes from the survey row's settings line, not from the occupied * slots: on complete data the no-answer slot is never journaled, and * inferring from occupancy would shift every colour and the guide text local topk = `KM' - 2 if ustrregexm(`"`sflags'"', "top=([0-9]+)") { local topk = real(ustrregexs(1)) local KM = `topk' + 2 } local N = real(`"`NN'"') if `N' >= . | `N' <= 0 { di as err "surveymap paths: `using' has no survey row with the scope count" di as err " the journal is damaged; rewrite it with surveymap paths ..., nodraw" exit 459 } * ---- what to highlight ---------------------------------------------- * Two forms. highlight(paths #) picks out the # most common complete * paths: their ribbons keep full colour while every other ribbon fades, * and their table rows print bold. highlight(var = value) picks out one * answer block: the ribbons into and out of it stay, the rest fade. local hlmode "" if `"`highlight'"' != "" { local hspec = strtrim(`"`highlight'"') gettoken hw hrest : hspec, parse(" =") local hwl = strlower(strtrim(`"`hw'"')) if inlist("`hwl'", "path", "paths") { local hrest = strtrim(`"`hrest'"') if `"`hrest'"' == "" local hrest "1" capture confirm integer number `hrest' if _rc | `hrest' < 1 | `hrest' > 10 { di as err "highlight(paths #): give a whole number from 1 to 10" di as err " highlight(paths 3) keeps the three most common complete paths" exit 198 } if "`NPATH'" == "" { di as err "highlight(paths): this journal has no path table to pick from" exit 459 } local hlmode "paths" local hln = min(`hrest', `NPATH') local hlpct = 0 forvalues r = 1/`hln' { if `"`sq_`r''"' == "" continue local hlpct = `hlpct' + real(strtrim(`"`sp_`r''"')) local rest `"`sq_`r''"' local t = 0 local prev . while `"`rest'"' != "" & `t' < `K' { local ++t local p = strpos(`"`rest'"', char(18)) if `p' { local tok = substr(`"`rest'"', 1, `p' - 1) local rest = substr(`"`rest'"', `p' + 1, .) } else { local tok `"`rest'"' local rest "" } local s = real(`"`tok'"') if `s' >= . continue local HB_`t'_`s' = 1 if `prev' < . local HF_`=`t'-1'_`prev'_`s' = 1 local prev = `s' } } local hlpct = string(`hlpct', "%9.1f") } else { local eq = strpos(`"`hspec'"', "=") if !`eq' { di as err `"highlight(): could not read "`hspec'""' di as err " highlight(paths 3) keeps the three most common complete paths" di as err " highlight(q2 = 1) keeps the ribbons through one answer block" exit 198 } local hv = strtrim(substr(`"`hspec'"', 1, `eq' - 1)) local hval = strtrim(substr(`"`hspec'"', `eq' + 1, .)) local t0 = 0 forvalues t = 1/`K' { if `"`v_`t''"' == `"`hv'"' local t0 = `t' } if !`t0' { di as err "highlight(): `hv' is not an item on this map" local its "" forvalues t = 1/`K' { local its "`its' `v_`t''" } di as err " the items are:`its'" exit 198 } local s0 = 0 forvalues s = 1/`KM' { if `"`vk_`t0'_`s''"' == "" continue if real(`"`vk_`t0'_`s''"') == real(`"`hval'"') & real(`"`hval'"') < . { local s0 = `s' } if `"`vk_`t0'_`s''"' == "~o" & inlist(strlower(`"`hval'"'), "other", "~o") local s0 = `s' if `"`vk_`t0'_`s''"' == "~m" & inlist(strlower(`"`hval'"'), "noanswer", "missing", "~m") local s0 = `s' } if !`s0' { di as err "highlight(): `hv' has no drawn block for `hval'" di as err " it is pooled or absent; raise top(), or name a drawn" di as err " answer value, or use other / noanswer for the grey blocks" exit 198 } local hlmode "block" local HB_`t0'_`s0' = 1 if `t0' < `K' { forvalues b = 1/`KM' { local HF_`t0'_`s0'_`b' = 1 } } if `t0' > 1 { forvalues a = 1/`KM' { local HF_`=`t0'-1'_`a'_`s0' = 1 } } } } * ---- geometry -------------------------------------------------------- * One vertical scale for every column: pixels per person. Block heights * get a floor so a sliver stays visible; the floor inflates a column by * a few pixels at most, and the ribbons anchor inside the drawn blocks, * so the braid still partitions each bar edge exactly. local PH = 520 local TOPM = 96 local BOTM = 30 local NW = 16 local GAPX = 186 local LM = 14 local RM = 200 local SGAP = 5 local pxper = (`PH' - (`KM' - 1) * `SGAP') / `N' local W = `LM' + `K' * `NW' + (`K' - 1) * `GAPX' + `RM' * block tops and heights; a slot with no row (n = 0) takes no space local Hmax = 0 forvalues t = 1/`K' { local y = `TOPM' forvalues s = 1/`KM' { if `"`nk_`t'_`s''"' == "" continue local h = max(3, round(`nk_`t'_`s'' * `pxper')) local gy_`t'_`s' = `y' local gh_`t'_`s' = `h' local y = `y' + `h' + `SGAP' } local Hmax = max(`Hmax', `y' - `SGAP') } local H = `Hmax' + `BOTM' * ---- file ------------------------------------------------------------ local stub `"`hf'"' local p = max(strrpos(`"`stub'"', "/"), strrpos(`"`stub'"', char(92))) if `p' local stub = substr(`"`stub'"', `p' + 1, .) if strlower(substr(`"`stub'"', -5, .)) == ".html" { local stub = substr(`"`stub'"', 1, strlen(`"`stub'"') - 5) } _srf_slug `"`stub'"' local PFX "`s(o)'" local jname `"`name'"' if `"`jname'"' == "" { local jname `"`using'"' local p = max(strrpos(`"`jname'"', "/"), strrpos(`"`jname'"', char(92))) if `p' local jname = substr(`"`jname'"', `p' + 1, .) } local ttl `"`title'"' if `"`ttl'"' == "" local ttl "How the answers flow, item by item" * option text and filenames arrive as literals, not marker-encoded * journal text, so they get their own XML escaping here _srf_lit `"`ttl'"' local ttl `"`s(o)'"' _srf_lit `"`jname'"' local jname `"`s(o)'"' tempname h quietly file open `h' using `"`hf'"', write text replace if "`embed'" == "" { file write `h' `""' _n file write `h' `""' _n file write `h' `""' _n file write `h' `""' _n _srf_map `"`ttl'"' file write `h' `"surveymap · `s(o)'"' _n file write `h' `""' _n if "`embed'" == "" { file write `h' `""' _n file write `h' `""' _n _srf_map `"`ttl'"' file write `h' `"

`s(o)'

"' _n } file write `h' `"
"' _n * caption: what this is, whose numbers, which scope _srf_n `"`NN'"' local Nfmt "`s(o)'" file write `h' `"

`Nfmt' respondents, each counted once in every column · journal: `jname'

"' _n if `"`scopetxt'"' != "" { _srf_map `"`scopetxt'"' file write `h' `"

scope: only respondents where `s(o)' · every count and share on this page describes that group

"' _n } file write `h' `"

A column is one item. A block is one answer, sized by how many gave it. A ribbon joins two answers on consecutive items, sized by how many gave both. Hover anything for exact counts.

"' _n if "`hlmode'" == "paths" { local hlw "the `hln' most common complete paths, together" if `hln' == 1 local hlw "the most common complete path," local hlnote "" if real(strtrim("`hlpct'")) < 5 { local hlnote " That is a small slice of the sample, so the fade mutes most of the braid; highlight(var = value) picks out one answer block instead." di as txt "note: the highlighted paths cover only " as res strtrim("`hlpct'") "%" as txt " of the sample; consider highlight(var = value)" } file write `h' `"

highlighted: `hlw' `=strtrim("`hlpct'")'% of respondents; every other ribbon is faded, and the picked paths print bold in the table below.`hlnote'

"' _n } if "`hlmode'" == "block" { _srf_map `"`dk_`t0'_`s0''"' file write `h' `"

highlighted: the ribbons into and out of `hv' = `s(o)'; every other ribbon is faded

"' _n } file write `h' `"
"' _n file write `h' `""' _n file write `h' `"Response-flow map of `K' survey items for `Nfmt' respondents. Each item is a column of blocks, one per common answer plus other answers and no answer. Ribbons between columns carry the respondents who gave both answers."' _n * ---- ribbons first, so bars and labels overprint them ---------------- * Each bar edge is partitioned exactly: outgoing ribbons in to-slot * order on the right edge, incoming ribbons in from-slot order on the * left edge, each taking its share of the drawn block height. forvalues t = 1/`=`K'-1' { local u = `t' + 1 local xa = `LM' + (`t' - 1) * (`NW' + `GAPX') + `NW' local xb = `LM' + (`u' - 1) * (`NW' + `GAPX') local xm = (`xa' + `xb') / 2 * running offsets down each block edge forvalues s = 1/`KM' { local oa_`s' = 0 local ob_`s' = 0 } forvalues a = 1/`KM' { forvalues b = 1/`KM' { if `"`fn_`t'_`a'_`b''"' == "" continue local n = `fn_`t'_`a'_`b'' local ha = `gh_`t'_`a'' * `n' / `nk_`t'_`a'' local hb = `gh_`u'_`b'' * `n' / `nk_`u'_`b'' local ya = `gy_`t'_`a'' + `oa_`a'' local yb = `gy_`u'_`b'' + `ob_`b'' local oa_`a' = `oa_`a'' + `ha' local ob_`b' = `ob_`b'' + `hb' local ya2 = `ya' + `ha' local yb2 = `yb' + `hb' foreach c in ya yb ya2 yb2 { local `c' = round(``c'', .1) } _srf_color `a' `topk' local col "`s(o)'" local op ".40" if `a' == `KM' | `b' == `KM' local op ".22" local strk "none" local sw "" if "`hlmode'" != "" { * a highlight fades everything it does not pick out if "`HF_`t'_`a'_`b''" == "1" { local op ".78" local strk "`col'" local sw `" stroke-width=".8""' } else { local col "#d4d4d4" local op ".20" } } file write `h' `""' _n file write `h' `""' _srf_n "`n'" local nf "`s(o)'" _srf_wtip `h' `"`v_`t'' `dk_`t'_`a'' -> `v_`u'' `dk_`u'_`b''"' _srf_wtip `h' `"`nf' of `Nfmt' (`=strtrim("`fp_`t'_`a'_`b''")'% of respondents in scope)"' file write `h' `""' _n } } } * ---- blocks and their labels ----------------------------------------- forvalues t = 1/`K' { local x = `LM' + (`t' - 1) * (`NW' + `GAPX') * column header: item name, wording, in the band above the blocks _srf_mell 24 `"`v_`t''"' _srf_map `"`s(o)'"' file write `h' `"`s(o)'"' _n _srf_wrapn 30 3 `"`lb_`t''"' local nl = `s(n)' forvalues li = 1/`nl' { local yy = 20 + 13 * `li' _srf_map `"`s(l`li')'"' file write `h' `"`s(o)'"' _n } forvalues s = 1/`KM' { if `"`nk_`t'_`s''"' == "" continue local y = `gy_`t'_`s'' local hh = `gh_`t'_`s'' _srf_color `s' `topk' local col "`s(o)'" local bstrk "" if "`HB_`t'_`s''" == "1" { local bstrk `" stroke="#1f2d3a" stroke-width="1.6""' } file write `h' `""' _n file write `h' `""' _srf_n "`nk_`t'_`s''" local nf "`s(o)'" _srf_wtip `h' `"`v_`t'' `dk_`t'_`s''"' _srf_wtip `h' `"`nf' of `Nfmt' (`=strtrim("`pk_`t'_`s''")'% of respondents in scope)"' file write `h' `""' _n if `hh' >= 12 { _srf_mell 22 `"`dk_`t'_`s''"' _srf_map `"`s(o)'"' local lt "`s(o)'" local ly = `y' + `hh' / 2 + 3.5 local ly = round(`ly', .1) local lx = `x' + `NW' + 5 local pc = strtrim(`"`pk_`t'_`s''"') file write `h' `"`pc'% `lt'"' _n } } } file write `h' `""' _n file write `h' `"
"' _n * ---- the largest flows, visible: hover is never the only way --------- local nfl = 0 forvalues t = 1/`=`K'-1' { forvalues a = 1/`KM' { forvalues b = 1/`KM' { if `"`fn_`t'_`a'_`b''"' == "" continue local ++nfl local fl`nfl' "`fn_`t'_`a'_`b'' `t' `a' `b'" } } } local nshow = min(8, `nfl') if `nshow' > 0 { file write `h' `"

The largest flows, each counting the people who gave one pair of answers on two neighboring items:

"' _n file write `h' `""' _n forvalues r = 1/`nshow' { local best = 0 local bi = 0 forvalues i = 1/`nfl' { if "`fl`i''" == "" continue gettoken bn rest : fl`i' if `bn' > `best' { local best = `bn' local bi = `i' } } if !`bi' continue gettoken bn rest : fl`bi' gettoken bt rest : rest gettoken ba rest : rest gettoken bb rest : rest local fl`bi' "" local bu = `bt' + 1 _srf_n "`bn'" local nf "`s(o)'" local pc = strtrim(`"`fp_`bt'_`ba'_`bb''"') _srf_mell 22 `"`dk_`bt'_`ba''"' _srf_map `"`s(o)'"' local la "`s(o)'" _srf_mell 22 `"`dk_`bu'_`bb''"' _srf_map `"`s(o)'"' local lb "`s(o)'" _srf_map `"`v_`bt''"' local va "`s(o)'" _srf_map `"`v_`bu''"' local vb "`s(o)'" file write `h' `""' _n } file write `h' "
sharepeopleflow
`pc'%`nf'`va' `la' → `vb' `lb'
" _n } * ---- the most common full sequences ---------------------------------- if "`NPATH'" != "" { local order "" forvalues t = 1/`K' { _srf_map `"`v_`t''"' if `t' == 1 local order "`s(o)'" else local order "`order' → `s(o)'" } * the claim needs the singleton-interview count the writer journals, * not the distinct-route count: with N interviews on N/2 routes of * two people each, every route is shared and the sentence would lie local fragtxt "" if "`NUNI'" != "" { if `NUNI' >= 0.5 * `N' { _srf_n "`NUNI'" local fragtxt " `s(o)' of the `Nfmt' interviews follow a unique route, so no single complete route is common. The ribbons above show the structure." } } file write `h' `"

The most common full paths, reading `order'.`fragtxt'

"' _n file write `h' `""' _n forvalues r = 1/`NPATH' { if `"`sq_`r''"' == "" continue local cells "" local rest `"`sq_`r''"' local t = 0 while `"`rest'"' != "" & `t' < `K' { local ++t local p = strpos(`"`rest'"', char(18)) if `p' { local tok = substr(`"`rest'"', 1, `p' - 1) local rest = substr(`"`rest'"', `p' + 1, .) } else { local tok `"`rest'"' local rest "" } local s = real(`"`tok'"') if `s' >= . continue local lab `"`dk_`t'_`s''"' _srf_mell 22 `"`lab'"' _srf_map `"`s(o)'"' if `t' == 1 local cells "`s(o)'" else local cells `"`cells' → `s(o)'"' } _srf_n `"`sn_`r''"' local nf "`s(o)'" local pc = strtrim(`"`sp_`r''"') local trc "" if "`hlmode'" == "paths" { if `r' <= `hln' local trc `" class="hl""' } file write `h' `""' _n } file write `h' `"
sharepeoplepath
`pc'%`nf'`cells'
"' _n } * ---- how to read it, in this map's own names ------------------------- file write `h' `"
How to read this map, step by step
    "' _n _srf_map `"`v_1'"' local v1 "`s(o)'" _srf_mell 40 `"`dk_1_1'"' _srf_map `"`s(o)'"' local d11 "`s(o)'" local p11 = strtrim(`"`pk_1_1'"') * say the grey block exists only where one is drawn: a map of items * everybody answered has none, and column one may not have one either local greysent "" forvalues t = 1/`K' { if `"`nk_`t'_`KM''"' != "" { local greysent " Light grey blocks collect everyone with no answer recorded on that item." continue, break } } file write `h' `"
  1. Start at the left. The first column is `v1'. The column stacks all `Nfmt' respondents: each colored block is one answer, its label gives the share who gave it, and the top block here is `d11' at `p11'%.`greysent'
  2. "' _n file write `h' `"
  3. Follow a ribbon to the right. A ribbon connects an answer on one item to an answer on the next, and its thickness is the number of people who gave both. Hovering a ribbon shows the exact count. Where a block fans out into several ribbons, the sample is splitting. Where ribbons converge on one block, paths are merging.
  4. "' _n file write `h' `"
  5. Blocks labelled other answers pool everything past the `topk' most common. Nothing is dropped: every respondent sits in exactly one block of every column, so each column adds back to `Nfmt'.
  6. "' _n if "`NPATH'" != "" { file write `h' `"
  7. Each line of the table under the figure is one complete path from the first item to the last, with the number and share of respondents who took exactly that path. The ribbons above join two items at a time. The table lines run the whole way across.
  8. "' _n } if `"`scopetxt'"' != "" { _srf_map `"`scopetxt'"' file write `h' `"
  9. This map is scoped: only respondents where `s(o)' are counted, in every column, block and ribbon.
  10. "' _n } file write `h' `"
"' _n file write `h' `"

Reading rule: counts are unweighted respondents in scope. A ribbon is the number of people who gave that pair of answers on two consecutive items. A table line is the number who took that complete path. no answer recorded includes people the routing never showed the item, which item data cannot separate from a decline. Drawn by surveymap from `jname'. The journal has a weighted count column when the scan was weighted.

"' _n file write `h' `"
"' _n if "`embed'" == "" { file write `h' `""' _n file write `h' `""' _n } file close `h' sreturn local out `"`hf'"' di as txt "surveymap paths: map written to " as res `"`hf'"' end * slot color: ranked answers get the palette, other and no answer grey program define _srf_color, sclass args s topk sreturn clear if `s' > `topk' + 1 { sreturn local o "#e8e8e8" exit } if `s' > `topk' { sreturn local o "#bfbfbf" exit } local c1 "#39566e" local c2 "#6e8ca8" local c3 "#a3b8cc" local c4 "#c98f4a" local c5 "#7a9b6d" local c6 "#9b7a9b" sreturn local o "`c`s''" end * ---- helpers: local copies of the html renderer's text machinery --------- * (own copies, so this renderer does not depend on another file's internals) * escape LITERAL XML-special characters -> s(o). For option text and * filenames, which never pass through the journal's marker encoding. program define _srf_lit, sclass args t local t = subinstr(`"`t'"', "&", "&", .) local t = subinstr(`"`t'"', "<", "<", .) local t = subinstr(`"`t'"', ">", ">", .) local t = subinstr(`"`t'"', char(34), """, .) sreturn local o `"`t'"' end * map internal marker chars to XML entities -> s(o) program define _srf_map, sclass args t local t = subinstr(`"`t'"', char(16), "&", .) local t = subinstr(`"`t'"', char(17), "<", .) local t = subinstr(`"`t'"', char(18), ">", .) local t = subinstr(`"`t'"', char(19), """, .) local t = subinstr(`"`t'"', char(6), "`", .) local t = subinstr(`"`t'"', char(7), "$", .) local t = subinstr(`"`t'"', char(1), "…", .) local t = subinstr(`"`t'"', char(3), "·", .) sreturn local o `"`t'"' end * one tooltip line (a real newline: browsers honour it inside svg ) program define _srf_wtip gettoken h 0 : 0 * the text arrives as ONE compound-quoted token; gettoken binds and * strips the quotes (expanding `0' directly keeps them, and they draw) gettoken txt : 0 _srf_map `"`txt'"' local t = trim(`"`s(o)'"') file write `h' `"`t'"' _n end * comma-format a count string ("." or "" -> empty) -> s(o) program define _srf_n, sclass args s if `"`s'"' == "." | `"`s'"' == "" sreturn local o "" else sreturn local o = trim(string(real(`"`s'"'), "%20.0fc")) end * middle-ellipsis to maxlen -> s(o); only when the label genuinely overflows program define _srf_mell, sclass args maxlen t * character counts, not bytes: a byte-based cut can split a curly quote * or an accented letter in half and leave an invalid byte on the page if ustrlen(`"`t'"') <= `maxlen' { sreturn local o `"`t'"' exit } local h1 = floor((`maxlen'-1)/2) local h2 = `maxlen' - 1 - `h1' local L = ustrlen(`"`t'"') sreturn local o = usubstr(`"`t'"', 1, `h1') + char(1) + /// usubstr(`"`t'"', `L' - `h2' + 1, .) end * word-wrap to at most maxlines lines -> s(n), s(l1..); overflow past the * last line merges in with a middle-ellipsis program define _srf_wrapn, sclass args maxlen maxlines t local rest = trim(`"`t'"') local n 0 while `"`rest'"' != "" & `n' < `maxlines' { if ustrlen(`"`rest'"') <= `maxlen' { local ++n local l`n' `"`rest'"' local rest "" } else { local head = usubstr(`"`rest'"', 1, `maxlen') local p = ustrrpos(`"`head'"', " ") if `p' < 8 local p = `maxlen' local ++n local l`n' = trim(usubstr(`"`rest'"', 1, `p')) local rest = trim(usubstr(`"`rest'"', `p'+1, .)) } } if `"`rest'"' != "" & `n' > 0 { local merged `"`l`n'' `rest'"' _srf_mell `maxlen' `"`merged'"' local l`n' `"`s(o)'"' } sreturn clear sreturn local n = `n' forvalues k = 1/`n' { sreturn local l`k' `"`l`k''"' } end * sanitize an id/class prefix -> s(o) program define _srf_slug, sclass args t local out "" local L = strlen(`"`t'"') if `L' > 40 local L 40 forvalues i = 1/`L' { local ch = substr(`"`t'"', `i', 1) if !ustrregexm("`ch'", "^[A-Za-z0-9-]$") local ch "-" local out "`out'`ch'" } while substr("`out'", -1, 1) == "-" { local out = substr("`out'", 1, strlen("`out'")-1) } if substr("`out'", 1, 3) != "sm-" local out "sm-`out'" if "`out'" == "sm-" local out "sm-1" sreturn clear sreturn local o "`out'" end