๐Ÿ’พ Archived View for yretek.com โ€บ borradores โ€บ publica.lua captured on 2023-06-16 at 16:28:11.

View Raw

More Information

โฌ…๏ธ Previous capture (2023-04-19)

โžก๏ธ Next capture (2023-09-28)

-=-=-=-=-=-=-

--[[ 
  Examines the contents of the "programados" folder, determines
  which are to be published the day this script is run.
  Those who are get moved to the "articulos" directory
  Links are then inserted into index files
  This is meant for my capsule only. Nevertheless the code is
  offered as reference.
  
  These are gemtext files which filenames follow the 
  format of yyyy-mm-dd_file_title.gmi
  
  ยท-ยท-ยท Licence ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-
  This program is free software: you can redistribute it 
  and/or modify it under the terms of the GNU 
  General Public License as published by the Free Software 
  Foundation, either version 3 of the License, or (at your option) 
  any later version.
    This program is distributed in the hope that it will be 
  useful, but WITHOUT ANY WARRANTY; without even the implied 
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  See the GNU General Public License for more details.

  For A copy of the GNU General Public License along see 
  <https://www.gnu.org/licenses/>. 
]]
function read_text_file(f_name)
-- Takes the contents from f_name
-- Returns it as a string t_str
  f_ile = io.open(f_name, 'r')
  t_str = f_ile:read('*all')
  f_ile:close()
  return t_str
end

function del_rpt_consecutive_lines(in_parg, p_line, out_parg)
  -- deletes consecutive repeated lines in a paragraph
  p_line = p_line or ""  -- previous line
  in_parg = in_parg or "" -- in paragraph
  out_parg = out_parg or "" -- out paragraph
  fl,ol = split_paragraph(in_parg)
  if p_line ~= fl then out_parg = out_parg..fl..'\n' end
  p_line = fl
  in_parg = ol
  if in_parg ~= "" then
    return del_rpt_consecutive_lines(in_parg, p_line, out_parg)
  else
    return out_parg
  end
end

function insert_in_str(target_str, mark_str, insert_str)
  -- inserta insert_str in "target_str" after mark_str 
  return string.gsub(target_str, 
                     mark_str,
                     mark_str..insert_str,
                             1)
end

function split_paragraph(in_str)
-- Splits a paragraph into first and other_lines
   local i = string.find(in_str,'\n')
   if i ~= nil then
     local first_line = string.sub(in_str,1,i-1)
     local other_lines = string.sub(in_str,i+1,-1)
     return first_line, other_lines
   else
     return in_str, ""
   end
end  

function get_title(f_name)
  local in_str = read_text_file(f_name)
  f_line, _ = split_paragraph(in_str)
  return string.sub(f_line,14,-1)
end

function make_link(article_name, title_str)
  -- Makes a suscribible link line in the shape of
  -- gemini://whatever/folder/file.gmi 2022-01-01 Title
  folder_url  = "gemini://yretek.com/articulos/" 
  full_url = folder_url .. article_name
  return "=> " .. full_url .. " " .. title_str
end

function update_index(index_f_name, insert_point, link_str)
  local old_index = read_text_file(index_f_name)
  local new_index = insert_in_str(old_index, 
                             insert_point, 
                             link_str ) 
  new_index = del_rpt_consecutive_lines(new_index)
  write_text_file(index_f_name, new_index)
end

function update_indexes(link_str)
  update_index("../index.gmi", "## Artรญculos\n", link_str)
  update_index("../articulos/index.gmi","ยท\n", link_str)
  update_index("../todo.gmi","ยท\n", link_str)
end

function write_text_file(f_name, texto)
  -- Writes texto on file f_name
  local f_ile = io.open(f_name, 'w')
  io.output(f_ile)
  io.write(texto)
  io.close(f_ile)
end
-- .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

-- backing up old indexes
os.execute("cp ../index.gmi backup/index_prncpl.bak")
os.execute("cp ../articulos/index.gmi backup/index_artcls.bak")
os.execute("cp ../todo.gmi backup/todo.bak")

local scheduled_dir = "programados/"
local today_str = os.date('%Y-%m-%d')
local link_str = ""
local articulos = "../articulos/"

local scheduled_dir_contents = io.popen("ls "..scheduled_dir):read("*a")
print(scheduled_dir_contents)

-- Table to store the lines
local scheduled_dir_lines = {}

-- Iterate over the lines in the string and store them in the table
for line in scheduled_dir_contents:gmatch("[^\r\n]+") do
  table.insert(scheduled_dir_lines, line)
end

-- Examine entries
for i, article_name in ipairs(scheduled_dir_lines) do
  to_pub_date = string.sub(article_name,1,10)
  print(to_pub_date, i, article_name)
  if to_pub_date == today_str then
    print("Let's go", article_name)
    title_str = " "..to_pub_date..get_title("programados/"..article_name)
    link_str = make_link(article_name, title_str).."\n"
    -- mv artcl to articulos folder
    os.execute("mv "..scheduled_dir..article_name.." ".."../articulos/" )
    update_indexes(link_str)
    
  else
    print("No go", article_name)
  end
end