From 3ed787f1b5f095d251c8f388e424090e993068c7 Mon Sep 17 00:00:00 2001 From: ren Date: Fri, 23 Jun 2023 04:13:20 -0600 Subject: [PATCH] json parsing half implemented --- README.md | 17 ++++-- write.as.lua | 147 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 146 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 186f77a..404d76a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ # Roadmap + - [ ] For gemini to work properly we need only the title. Date, author are optional -- [ ] Make a script that properly formats the write as blog to .md hugo - - [ ] Use the json and/or csv metadata info - - [ ] date published - - [ ] Extract title from filename (regex parsing) - - [ ] We just need to append metadata and delete titles if it has +- [ ] Make a script that properly formats the write.as blog to .md hugo + - [ ] Use the json and/or csv metadata info + - [ ] date published + - [ ] Extract title from filename (regex parsing) + - [ ] We need #tags on body + - [ ] Title + - [ ] Date of creation + - [ ] Autor + - [ ] TranscripciĆ²n de lenguaje y fecha a validos por hugo + - [ ] Y al la forma con el YAML establecido que tiene ya + - [ ] We just need to append metadata and delete titles if it has diff --git a/write.as.lua b/write.as.lua index 732a748..8421f24 100755 --- a/write.as.lua +++ b/write.as.lua @@ -1,7 +1,8 @@ -#!/bin/lua +#!/usr/bin/lua -- Dependencies local json = require ("dkjson") +local lfs = require ("lfs") -- maybe we don't really need this at all. -- These two are the variables you need to change: -- Specially the folder one. @@ -21,27 +22,147 @@ local diario = "Journal" local unready = "#nochitlakaj" --local f = io.open(io.read(), "r") -local f = io.open("/home/arendia/Downloads/lumin-202305131725.json", "r") + +-- PRINT TABLES + +function tprint (tbl, indent) + if not indent then indent = 0 end + local toprint = string.rep(" ", indent) .. "{\r\n" + indent = indent + 2 + for k, v in pairs(tbl) do + toprint = toprint .. string.rep(" ", indent) + if (type(k) == "number") then + toprint = toprint .. "[" .. k .. "] = " + elseif (type(k) == "string") then + toprint = toprint .. k .. "= " + end + if (type(v) == "number") then + toprint = toprint .. v .. ",\r\n" + elseif (type(v) == "string") then + toprint = toprint .. "\"" .. v .. "\",\r\n" + elseif (type(v) == "table") then + toprint = toprint .. tprint(v, indent + 2) .. ",\r\n" + else + toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n" + end + end + toprint = toprint .. string.rep(" ", indent-2) .. "}" + return toprint +end + + + +-- Lua Table View by Elertan +table.print = function(t, exclusions) + local nests = 0 + if not exclusions then exclusions = {} end + local recurse = function(t, recurse, exclusions) + indent = function() + for i = 1, nests do + io.write(" ") + end + end + local excluded = function(key) + for k,v in pairs(exclusions) do + if v == key then + return true + end + end + return false + end + local isFirst = true + for k,v in pairs(t) do + if isFirst then + indent() + print("|") + isFirst = false + end + if type(v) == "table" and not excluded(k) then + indent() + print("|-> "..k..": "..type(v)) + nests = nests + 1 + recurse(v, recurse, exclusions) + elseif excluded(k) then + indent() + print("|-> "..k..": "..type(v)) + elseif type(v) == "userdata" or type(v) == "function" then + indent() + print("|-> "..k..": "..type(v)) + elseif type(v) == "string" then + indent() + print("|-> "..k..": ".."\""..v.."\"") + else + indent() + print("|-> "..k..": "..v) + end + end + nests = nests - 1 + end + + nests = 0 + print("### START TABLE ###") + for k,v in pairs(t) do + print("root") + if type(v) == "table" then + print("|-> "..k..": "..type(v)) + nests = nests + 1 + recurse(v, recurse, exclusions) + elseif type(v) == "userdata" or type(v) == "function" then + print("|-> "..k..": "..type(v)) + elseif type(v) == "string" then + print("|-> "..k..": ".."\""..v.."\"") + else + print("|-> "..k..": "..v) + end + end + print("### END TABLE ###") +end + + + +---------------------------------------- + +-- JSON Play + +local f = io.open("posts.json", "r") local content = f:read("*all") f:close() -local str = [[ -{ - "numbers": [ 2, 3, -20.23e+2, -4 ], - "currency": "\u20AC" -} -]] - local obj, pos, err = json.decode (content, 1, nil) if err then print ("Error:", err) else - print ("email", obj.email) - for i = 1,#obj.collections do - print (i, obj.collections[i].posts[i]) - end + print ("Author: ", obj.username) + print ("collections_table", obj.collections) + for i = 1,#obj.collections do + local total_de_posts = obj.collections[i].total_posts + print ("Total posts: ", total_de_posts) + for j = 1, total_de_posts, 1 do + local date = obj.collections[i].posts[j].updated + local title = obj.collections[i].posts[j].slug + local text = obj.collections[i].posts[j].body + local authory = "author: 'Lumin'" + + -- Look for "A dream" and #dreamjournal + + --print (j, "Time: ", obj.collections[i].posts[j].updated) -- Time is well set + --print (j, "Titulo: ", obj.collections[i].posts[j].slug) -- needs parsing + --print (j, "Texto: ", obj.collections[i].posts[j].body) -- Raw and good -- needs tag parsing + + -- We need to compose the string/file now properly and that's it. That is all in archivo. + + local archivo = date .. "\n" .. authory .. "\n" .. text + local filename = "mad-dirty/".. title .. ".md" + + local filewrite = assert(io.open(filename, "w")) + filewrite:write(archivo) + filewrite:close() + end + end end +-------------------------------------- + function replace(file, sA) -- -- Read the file