diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua | 339 |
1 files changed, 119 insertions, 220 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua b/Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua index 71207975d2f..d66eab069bb 100644 --- a/Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua +++ b/Master/texmf-dist/tex/context/base/mkiv/meta-tex.lua @@ -6,262 +6,161 @@ if not modules then modules = { } end modules ['meta-tex'] = { license = "see context related readme files" } -local tostring = tostring -local format, gsub, find, match = string.format, string.gsub, string.find, string.match +local tostring, tonumber = tostring, tonumber +local format = string.format local formatters = string.formatters local P, S, R, C, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cs, lpeg.match -metapost = metapost or { } +metapost = metapost or { } +local metapost = metapost +local context = context local implement = interfaces.implement --- local left = P("[") --- local right = P("]") --- local space = P(" ") --- local argument = left * C((1-right)^1) * right --- local pattern = (argument + space)^0 +do --- function metapost.sometxt(optional,str) --- if optional == "" then --- context.sometxta(str) --- else --- local one, two = lpegmatch(pattern,optional) --- if two then --- context.sometxtc(one,two,str) --- elseif one then --- context.sometxtb(one,str) --- else --- context.sometxta(str) --- end --- end --- end + local pattern = Cs((P([[\"]]) + P([["]])/"\\quotedbl{}" + P(1))^0) -- or \char -local pattern = Cs((P([[\"]]) + P([["]])/"\\quotedbl{}" + P(1))^0) -- or \char - -function metapost.escaped(str) - context(lpegmatch(pattern,str)) -end - -implement { - name = "metapostescaped", - actions = metapost.escaped, - arguments = "string" -} - -local simplify = true - --- local function strip(n,e) --- -- get rid of e(0) --- -- get rid of e(+*) --- e = gsub(e,"^+","") --- -- remove leading zeros --- e = gsub(e,"^([+-]*)0+(%d)","%1%2") --- if not simplify then --- -- take it as it is --- elseif n == "1" then --- return format("10^{%s}",e) --- end --- return format("%s\\times10^{%s}",n,e) --- end --- --- function metapost.format_n(fmt,...) --- fmt = gsub(fmt,"@","%%") --- local initial, hasformat, final = match(fmt,"^(.-)(%%.-[%a])(.-)$") --- if hasformat then --- str = format(fmt,...) --- str = gsub(str,"(.-)e(.-)$",strip) --- str = format("%s\\mathematics{%s}%s",initial,str,final) --- elseif not find(fmt,"%%") then --- str = format("%"..fmt,...) --- str = gsub(str,"(.-)e(.-)$",strip) --- str = format("\\mathematics{%s}",str) --- end --- context(str) --- end - --- todo: proper lpeg - --- local function strip(n,e) --- -- get rid of e(0) --- -- get rid of e(+*) --- e = gsub(e,"^+","") --- -- remove leading zeros --- e = gsub(e,"^([+-]*)0+(%d)","%1%2") --- if not simplify then --- -- take it as it is --- elseif n == "1" then --- return format("\\mathematics{10^{%s}}",e) --- end --- return format("\\mathematics{%s\\times10^{%s}}",n,e) --- end --- --- function metapost.format_n(fmt,...) --- fmt = gsub(fmt,"@","%%") --- if find(fmt,"%%") then --- str = format(fmt,...) --- else -- yes or no --- str = format("%"..fmt,...) --- end --- str = gsub(str,"([%-%+]-[%.%d]+)e([%-%+]-[%.%d]+)",strip) --- context(str) --- end --- --- function metapost.format_v(fmt,str) --- metapost.format_n(fmt,metapost.untagvariable(str,false)) --- end + function metapost.escaped(str) + context(lpegmatch(pattern,str)) + end --- -- -- + implement { + name = "metapostescaped", + actions = metapost.escaped, + arguments = "string" + } -local number = C((S("+-")^0 * R("09","..")^1)) -local enumber = number * S("eE") * number +end -local cleaner = Cs((P("@@")/"@" + P("@")/"%%" + P(1))^0) +do -context = context or { exponent = function(...) print(...) end } + local simplify = true + local number = C((S("+-")^0 * R("09","..")^1)) + local enumber = number * S("eE") * number + local cleaner = Cs((P("@@")/"@" + P("@")/"%%" + P(1))^0) -function metapost.format_string(fmt,...) - context(lpegmatch(cleaner,fmt),...) -end - -function metapost.format_number(fmt,num) - if not num then - num = fmt - fmt = "%e" + local function format_string(fmt,...) + context(lpegmatch(cleaner,fmt),...) end - local number = tonumber(num) - if number then - local base, exponent = lpegmatch(enumber,formatters[lpegmatch(cleaner,fmt)](number)) - if base and exponent then - context.MPexponent(base,exponent) + + local function format_number(fmt,num) + if not num then + num = fmt + fmt = "%e" + end + local number = tonumber(num) + if number then + local base, exponent = lpegmatch(enumber,formatters[lpegmatch(cleaner,fmt)](number)) + if base and exponent then + context.MPexponent(base,exponent) + else + context(number) + end else - context(number) + context(tostring(num)) end - else - context(tostring(num)) end -end - --- This is experimental and will change! - -function metapost.svformat(fmt,str) - metapost.format_string(fmt,metapost.untagvariable(str,false)) -end -function metapost.nvformat(fmt,str) - metapost.format_number(fmt,metapost.untagvariable(str,false)) -end - -implement { name = "metapostformatted", actions = metapost.svformat, arguments = { "string", "string" } } -implement { name = "metapostgraphformat", actions = metapost.nvformat, arguments = { "string", "string" } } + -- This is experimental and will change! --- kind of new + metapost.format_string = format_string + metapost.format_number = format_number -local f_exponent = formatters["\\MPexponent{%s}{%s}"] + function metapost.svformat(fmt,str) + format_string(fmt,metapost.untagvariable(str,false)) + end -local mpformatters = table.setmetatableindex(function(t,k) - local v = formatters[lpegmatch(cleaner,k)] - t[k] = v - return v -end) + function metapost.nvformat(fmt,str) + format_number(fmt,metapost.untagvariable(str,false)) + end -function metapost.texexp(num,bfmt,efmt) - local number = tonumber(num) - if number then - local base, exponent = lpegmatch(enumber,format("%e",number)) - if base and exponent then - if bfmt then - -- base = formatters[lpegmatch(cleaner,bfmt)](base) - base = mpformatters[bfmt](base) - else - base = format("%f",base) - end - if efmt then - -- exponent = formatters[lpegmatch(cleaner,efmt)](exponent) - exponent = mpformatters[efmt](exponent) + local f_exponent = formatters["\\MPexponent{%s}{%s}"] + + -- can be a weak one: mpformatters + + local mpformatters = table.setmetatableindex(function(t,k) + local v = formatters[lpegmatch(cleaner,k)] + t[k] = v + return v + end) + + function metapost.texexp(num,bfmt,efmt) + local number = tonumber(num) + if number then + local base, exponent = lpegmatch(enumber,format("%e",number)) + if base and exponent then + if bfmt then + -- base = formatters[lpegmatch(cleaner,bfmt)](base) + base = mpformatters[bfmt](base) + else + base = format("%f",base) + end + if efmt then + -- exponent = formatters[lpegmatch(cleaner,efmt)](exponent) + exponent = mpformatters[efmt](exponent) + else + exponent = format("%i",exponent) + end + return f_exponent(base,exponent) + elseif bfmt then + -- return formatters[lpegmatch(cleaner,bfmt)](number) + return mpformatters[bfmt](number) else - exponent = format("%i",exponent) + return number end - return f_exponent(base,exponent) - elseif bfmt then - -- return formatters[lpegmatch(cleaner,bfmt)](number) - return mpformatters[bfmt](number) else - return number + return num end - else - return num end -end - --- not in context a namespace - -if _LUAVERSION < 5.2 then - utilities.strings.formatters.add(formatters,"texexp", [[texexp(...)]], "local texexp = metapost.texexp") -else - utilities.strings.formatters.add(formatters,"texexp", [[texexp(...)]], { texexp = metapost.texexp }) -end --- print(string.formatters["%!3.3!texexp!"](10.4345E30)) --- print(string.formatters["%3!texexp!"](10.4345E30,"%2.3f","%2i")) --- print(string.formatters["%2!texexp!"](10.4345E30,"%2.3f")) --- print(string.formatters["%1!texexp!"](10.4345E30)) --- print(string.formatters["%!texexp!"](10.4345E30)) + implement { + name = "metapostformatted", + actions = metapost.svformat, + arguments = { "string", "string" } + } --- local function test(fmt,n) --- logs.report("mp format test","fmt: %s, n: %s, result: %s, \\exponent{%s}{%s}",fmt,n, --- formatters[lpegmatch(cleaner,fmt)](n), --- lpegmatch(enumber,formatters[lpegmatch(cleaner,fmt)](n)) --- ) --- end --- --- test("@j","1e-8") --- test("@j",1e-8) --- test("@j","1e+8") --- test("@j","1e-10") --- test("@j",1e-10) --- test("@j","1e+10") --- test("@j","1e-12") --- test("@j","1e+12") --- test("@j","1e-0") --- test("@j","1e+0") --- test("@j","1") --- test("@j test","1") --- test("@j","-1") --- test("@j","1e-102") --- test("@1.4j","1e+102") --- test("@j","1.2e+102") --- test("@j","1.23e+102") --- test("@j","1.234e+102") + implement { + name = "metapostgraphformat", + actions = metapost.nvformat, + arguments = { "string", "string" } + } -local f_textext = formatters[ [[textext("%s")]] ] -local f_mthtext = formatters[ [[textext("\mathematics{%s}")]] ] -local f_exptext = formatters[ [[textext("\mathematics{%s\times10^{%s}}")]] ] + if LUAVERSION < 5.2 then + utilities.strings.formatters.add(formatters,"texexp", [[texexp(...)]], "local texexp = metapost.texexp") + else + utilities.strings.formatters.add(formatters,"texexp", [[texexp(...)]], { texexp = metapost.texexp }) + end --- local cleaner = Cs((P("\\")/"\\\\" + P("@@")/"@" + P("@")/"%%" + P(1))^0) + local f_textext = formatters[ [[textext("%s")]] ] + local f_mthtext = formatters[ [[textext("\mathematics{%s}")]] ] + local f_exptext = formatters[ [[textext("\mathematics{%s\times10^{%s}}")]] ] -local mpprint = mp.print + local mpprint = mp.print -function mp.format(fmt,str) -- bah, this overloads mp.format in mlib-lua.lua - fmt = lpegmatch(cleaner,fmt) - mpprint(f_textext(formatters[fmt](metapost.untagvariable(str,false)))) -end + function mp.format(fmt,str) -- bah, this overloads mp.format in mlib-lua.lua + fmt = lpegmatch(cleaner,fmt) + mpprint(f_textext(formatters[fmt](metapost.untagvariable(str,false)))) + end -function mp.formatted(fmt,...) -- svformat - fmt = lpegmatch(cleaner,fmt) - mpprint(f_textext(formatters[fmt](...))) -end + function mp.formatted(fmt,...) -- svformat + fmt = lpegmatch(cleaner,fmt) + mpprint(f_textext(formatters[fmt](...))) + end -function mp.graphformat(fmt,num) -- nvformat - fmt = lpegmatch(cleaner,fmt) - local number = tonumber(num) - if number then - local base, exponent = lpegmatch(enumber,number) - if base and exponent then - mpprint(f_exptext(base,exponent)) + function mp.graphformat(fmt,num) -- nvformat + fmt = lpegmatch(cleaner,fmt) + local number = tonumber(num) + if number then + local base, exponent = lpegmatch(enumber,number) + if base and exponent then + mpprint(f_exptext(base,exponent)) + else + mpprint(f_mthtext(num)) + end else - mpprint(f_mthtext(num)) + mpprint(f_textext(tostring(num))) end - else - mpprint(f_textext(tostring(num))) end + end |