diff options
author | Justin Gassner <justin.gassner@mailbox.org> | 2024-02-16 09:41:26 +0100 |
---|---|---|
committer | Justin Gassner <justin.gassner@mailbox.org> | 2024-02-16 09:41:26 +0100 |
commit | a2671d2aa9a2e5f0595a6e16a4efa0b9d08fb93c (patch) | |
tree | fd026ba885cf5f0225cca8fe26331a0b53d7445b | |
download | bibpp-master.tar.zst |
-rwxr-xr-x | bibpp | 87 |
1 files changed, 87 insertions, 0 deletions
@@ -0,0 +1,87 @@ +#!/usr/bin/lua + +lpeg = require("lpeg") +inspect = require 'inspect' + +space = lpeg.S(" \t\r\n")^0 +brace_open = space * lpeg.S("{") +brace_close = space * lpeg.S("}") + +tag_name = space * lpeg.C( (lpeg.R("az") + lpeg.R("AZ"))^1 ) +tag_equals = space * lpeg.S("=") +tag_content_number = space * lpeg.C(lpeg.R("09")^1) +tag_content_string_braces = space * lpeg.P{ "{" * lpeg.Cs( ((1 - lpeg.S"{}") + lpeg.V(1))^0 ) * "}" } +tag_content_string_quotes = space * lpeg.S("\"") * lpeg.C( (1-lpeg.S("\""))^0 ) * lpeg.S("\"") +tag_content_string = tag_content_string_braces + tag_content_string_quotes +tag_content = tag_content_number + tag_content_string +tag = lpeg.Ct( tag_name * tag_equals * tag_content ) + +tag_separator = space * lpeg.S(",") +tag_list = lpeg.Ct( (tag_separator * tag)^1 * tag_separator^0 ) + +entry_at = space * lpeg.S("@") +entry_type = space * lpeg.C( (lpeg.R("az") + lpeg.R("AZ"))^1 ) +citation_key = space * lpeg.C( (1 - lpeg.S(","))^1 ) +entry = lpeg.Ct( entry_at * entry_type * brace_open * citation_key * tag_list * brace_close ) + +bib = lpeg.Ct( entry^0 ) --* -1 + +function prettyprint_entry_type(entry_type) + io.stdout:write(string.lower(entry_type)) +end +function prettyprint_citation_key(citation_key) + io.stdout:write(string.lower(citation_key)) +end + +function prettyprint_tag_name(tag_name) + io.stdout:write(string.lower(tag_name)) +end + +function prettyprint_tag_content(tag_content) + io.stdout:write('{') + io.stdout:write(tag_content) + io.stdout:write('}') +end + +function prettyprint_tag(tag, pad) + prettyprint_tag_name(tag[1]) + io.stdout:write(string.rep(' ', pad - #tag[1])) + io.stdout:write(' = ') + prettyprint_tag_content(tag[2]) +end + +function prettyprint_entry(entry) + io.stdout:write('@') + prettyprint_entry_type(entry[1]) + io.stdout:write('{') + prettyprint_citation_key(entry[2]) + io.stdout:write(',\n') + for i, tag in ipairs(entry[3]) do + io.stdout:write(' ') + prettyprint_tag(tag,10) + io.stdout:write(',\n') + end + io.stdout:write('}\n') +end + +function prettyprint(bibfile) + local firstentry = true + for _, entry in ipairs(bibfile) do + if not firstentry then + io.stdout:write('\n') + end + prettyprint_entry(entry) + firstentry = false + end +end + +file = io.open(arg[1]) +str = file:read("a") +file:close() + +result = bib:match(str) +if not result then + print('No Match') +end + +prettyprint(result) |