BastliPOSPrinter

A collection of programs that allow receipt printing
git clone git://xatko.vsos.ethz.ch/BastliPOSPrinter.git
Log | Files | Refs

commit 8d6b28b556324270f1c36c468887eaeb5777d426
parent 805844e27f926f0c5c187a3ce688179f44d84f1a
Author: Dominik Schmidt <doeme@bastli.ethz.ch>
Date:   Wed,  5 Sep 2018 00:37:28 +0200

Implementd JSON_to_GROFF with lua

Diffstat:
README.md | 3++-
pipeline/10_JSON_to_GROFF.d | 64----------------------------------------------------------------
pipeline/10_JSON_to_GROFF.lua | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
template.roff | 2++
4 files changed, 65 insertions(+), 65 deletions(-)

diff --git a/README.md b/README.md @@ -5,7 +5,8 @@ Requirements ------------ * amqp-tools -* A D-Compiler, e.g. ldc2 +* lua +* luajson * ansifilter * bash * groff diff --git a/pipeline/10_JSON_to_GROFF.d b/pipeline/10_JSON_to_GROFF.d @@ -1,64 +0,0 @@ -module jsontogroff; -import std.stdio; -import std.process; -import std.json; -import std.array; -import std.algorithm; -import std.regex; -import std.format; -import std.conv; -import std.range; -import std.datetime : Clock; - -void main(string[] argv){ - auto str=stdin.byChunk(4096).map!(a=>cast(char[])a).joiner(); - auto j=parseJSON(str); - - string positions(){ - Appender!string app; - app~=".na\n"; - app~=".TS\n"; - "llxni\n".repeat(j["articles"].array.length).copy(app); - app~="===\n"; - app~="llxbni\n"; - app~=".\n"; - double total=0; - foreach(ulong ix,art; j["articles"]){ - double price=art["price"].str.to!double; - string name=art["name"].str; - ulong amount=art["amount"].integer; - auto esc_name=name.replace("\\","\\\\").replace("\n","\n "); - app.formattedWrite!"%d\tT{\n%s\nT}\t%.2f\n"(amount,esc_name,price*amount); - total+=price * amount; - } - app~="\n"; - app.formattedWrite("\tTOTAL\t%.2f\n", total); - app~=".TE\n"; - app~=".al\n"; - return app.data; - } - string delegate()[string] tbl=[ - "sale_id": (){return j["sale_id"].integer.to!string;}, - "positions": &positions, - "today": (){return Clock.currTime.toISOExtString();}, - ]; - auto lookup(Captures!(char[]) cap){ - auto b=cap[1]; - auto fct=b in tbl; - if(!fct){ - return "{{"~b~"}}"; - } - else{ - return cast(const(char)[])(*fct)(); - } - } - File tmplate=File(environment.get("GROFF_TEMPLATE"), "r"); - scope(exit)tmplate.close(); - auto reg=ctRegex!("\\{\\{([^}]+)\\}\\}"); - auto sink=stdout.lockingTextWriter; - - foreach(line; tmplate.byLine(KeepTerminator.yes)){ - replaceAllInto!(lookup)(sink, line, reg); - } - -} diff --git a/pipeline/10_JSON_to_GROFF.lua b/pipeline/10_JSON_to_GROFF.lua @@ -0,0 +1,61 @@ +#!/usr/bin/lua + +local json = require("json") + +s = io.read("*a") + +local j=json.decode(s) + +local tpl=assert(io.open(os.getenv("GROFF_TEMPLATE"), "r")) + +function getPositions() + local str="===\nlrxbni\n.\n" + local str2=".TS\n" + local total=0 + for _,a in pairs(j["articles"]) do + local escname=a["name"] + local subtot=a["price"]*a["amount"] + total=total+subtot + str=str..string.format("%d\tT{\n%s\nT}\t%.2f\n", a["amount"], escname, subtot) + str2=str2.."llxni\n" + end + str=str..string.format("\n\tTOTAL\t%.2f\n", total) + return str2..str..".TE"; +end + +local fctbl={ + ["sale_id"]=function() + return j["sale_id"] + end, + ["positions"]=function() + return getPositions() + end, + ["today"]=function() + return os.date("%Y-%m-%d %H:%M") + end, +} + +function processLine(t) + a,b,name= string.find(t, "{{([^}]+)}}") + if name then + io.write(string.sub(t,1,a-1)) + if fctbl[name] then + io.write(fctbl[name]()) + else + io.write("{{"..name.."}}") + end + processLine(string.sub(t,b+1)) + else + io.write(t) + end +end + +while true do + t=tpl:read("*line") + if not t then + break + end + processLine(t.."\n") +end + +tpl:close() diff --git a/template.roff b/template.roff @@ -40,7 +40,9 @@ RECEIPT +.na {{positions}} +.ad AMIV an der ETH .br