๐Ÿ’พ Archived View for yretek.com โ€บ borradores โ€บ compone.lua captured on 2023-09-08 at 16:08:24.

View Raw

More Information

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

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

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

-- Compone
--[[ 
  Adds header, footer to a draft file,
  building a "blog" gemtext entry
  The resulting file is then written on the ../programados folder
  
  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/>. 
]]
-- Functions
function read_text_file(f_name)
-- Takes the contents from f_name Returns it as a string t_str
  local f_ile = io.open(f_name, 'r')
  local t_str = f_ile:read('*all')
  f_ile:close()
  return t_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

function ask_user(prompt_str)
-- prompts user prompt_str, gets answer
   io.stdout:write(prompt_str .. " > ")
   local answer_str = io.stdin:read()
   return answer_str
end

function is_file_here(file_name)
  local was_found = false
  i_file = io.open(file_name, i)
  if i_file~=nil then 
    io.close(i_file) 
    was_found = true 
  end
  return was_found
end

function ensures_dot_gmi(in_str)
-- Appends ".gmi" to in_str if in_str has no .gmi extension
  local out_str = in_str
  local ext = string.sub(in_str,-4,-1)
  if ext ~= ".gmi" then out_str = out_str..".gmi" end
  return out_str  
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 strip(in_str)
-- Strips spaces from in_str (right and left)
-- "  Hola mundo  " -> "Hola mundo"
   local r = string.find(in_str,"%s+\$")
   local out_str = in_str
   if r ~= nil then
     out_str = string.sub(in_str, 1, r-1)
   end
   local _, l = string.find(out_str,"^%s+")
   if l ~= nil then 
     out_str = string.sub(out_str, l+1,-1)
   end
   return out_str
end

function get_word_count(f_name)
  -- returns the word count from a file named f_name
  os.execute("wc -w " .. f_name .. " > .words.txt")
  local raw_wc_str = read_text_file(".words.txt")
  local wc_str = string.sub(raw_wc_str, string.find(raw_wc_str, "%d+%s"))
  return "* Palabras: "..wc_str.."\n"
end

--[[
ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-
 Main -----------------------------------------------
ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-ยท-
]]
scheduled_dir = "programados/"
today_str = os.date('%Y-%m-%d')

-- Gets draft file name from user
prompt_str = "ยฟCuรกl es el archivo de borrador? %%% Para cancelar"
repeat
  draft_file_str = ask_user(prompt_str)
  if draft_file_str == "%%%" then os.exit() end
  prompt_str = "Fichero no encontrado.\nUse otro nombre.\n%%% detiene el programa."
until is_file_here(draft_file_str)
-- Gets to_publication date from user
repeat 
  prompt_str = "Fecha de publicaciรณn, enter para hoy"
  raw_date_str = ask_user(prompt_str)
until string.len(raw_date_str) == 10 or raw_date_str == ""
-- minimum validation

if raw_date_str == "" then date_str = today_str else date_str = raw_date_str end


raw_text_str = read_text_file(draft_file_str)

title_str, draft_text_str = split_paragraph(raw_text_str)
title_str = strip(title_str)

if title_str == "" then title_str = "Sin tรญtulo" end
draft_text_str = strip(draft_text_str)
word_count_str = get_word_count(draft_file_str)

header_str = "# Yretek ๐Ÿƒ " .. title_str .. "\n\n"
footer_title_str = "\n\n### Pie de pรกgina" .. "\n"
date_field_str = "* Fecha: " .. date_str .. "\n"
author_str = [[
~ Miguel de Luis Espinosa
=> mailto:yretek@proton.me yretek@proton.me

]]
footer_str = footer_title_str .. date_field_str .. word_count_str .. author_str

to_write_str = header_str .. draft_text_str .. footer_str

out_file_name = date_str .. "_" .. draft_file_str 
out_file_name = ensures_dot_gmi(out_file_name)
write_text_file(out_file_name, to_write_str)

-- a last look at the entry
os.execute("vim "..out_file_name) 
-- moves entry to its directory
os.execute("mv "..out_file_name.." "..scheduled_dir )

-- Ta da!