cybersiderality/write.as.lua

237 lines
7.6 KiB
Lua
Executable File

#!/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.
--
-- Read the file
--
--local path = io.open(".path", "r")
local path
--folder = path:read("*all")
--path:close()
-- Discriminatory strings
local sA = "#dreamjournal"
local diario = "Journal"
local unready = "#nochitlakaj"
--local f = io.open(io.read(), "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 obj, pos, err = json.decode (content, 1, nil)
if err then
print ("Error:", err)
else
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
--
local f = io.open(file, "r")
local content = f:read("*all")
f:close()
--
-- Edit the string
--
--content = string.gsub(sA, "Hello", "Hello, ")
-- local Scontent = string.format("%s", content)
--print(Scontent)
--print(string.len(Scontent))
-- Checks for duplicates and already published files
nota=string.find(content, sA)
if nota~=nil then
print(file .. " > this note has already been published. Nothing to be done. 🌠 <")
elseif string.find(content, unready) then
print(file .. " > This note is not ready for publication yet ☄️☄️ <")
else
--
-- Write it out
--
local f = io.open(file, "w")
f:write(sA .. content)
f:close()
print(" 🌊 This has be set to public: "..file)
end
end
function scandir(directory)
local pfile = assert(io.popen(("find '%s' -mindepth 1 -maxdepth 1 -type d -printf '%%f\\0'"):format(directory), 'r'))
local list = pfile:read('*a')
pfile:close()
local folders = {}
for filename in string.gmatch(list, '[^%z]+') do
table.insert(folders, filename)
end
return folders
end
function dirLookup(dir)
--local p = io.popen('find "'..dir..'" -type f') --Open directory look for files, save data in p. By giving '-type f' as parameter, it returns all files.
local p = io.popen('ls '..dir) --Open directory look for files, save data in p. By giving '-type f' as parameter, it returns all files.
for file in p:lines() do --Loop through all files
gecko=string.find(file, ".md")
if string.match(file, diario) then
print(file .. " > 💫 This is a Journal and it will not be set to public 🦤 <")
elseif gecko~=nil then
--print(file .. " in spot " ..gecko) --debug
replace(dir..file, sA)
else
print(" 🍃 "..file.." <<<<<<<<<<<< This is not a markdown file it will be ignored")
end
end
end
-- Execution
--print("--------------------------------------------------------------------------------------\n This script will append on top the string: "..sA.." Which will make public all your markdown.md documents in the folder you specify next " .. "\n It will also avoid to publish any file that is named 'Journal'. \n--------------------------------------------------------------------------------------\n")
--print("Insert the path of the folder (defaults to the current one) where your notes are with the last backslash included since this doesn't use LuaFileSystem: (uses a bit of gnu-core-utils)")
--folder = io.read()
--print("-----------------------------------------------------------------\n Directory to be used is: " .. folder .. "\n-----------------------------------------------------------------\n")
--dirLookup(folder)
--scandir(folder)