Skip to content

Instantly share code, notes, and snippets.

@prppedro
Created January 22, 2022 17:50
Show Gist options
  • Save prppedro/9316e81ba3811976e7e9ebaac0210a77 to your computer and use it in GitHub Desktop.
Save prppedro/9316e81ba3811976e7e9ebaac0210a77 to your computer and use it in GitHub Desktop.
#!/bin/bash
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# #
# Conversor de TXT antigo para MD - Pedro T. R. Pinheiro - 22JAN22 #
# --------- -- --- ------ ---- -- - ----- -- -- -------- - ------- #
# #
# Este conversor transforma arquivos de texto puro por mim escritos #
# no passado em formato Markdown, para que possam ser corretamente #
# arquivados neste repositório. Provavelmente só é útil para mim, mas #
# seguramente pode ser adaptado a outros usos. #
# -- #
# Old TXT to MD converter: converts my old plain text files into the #
# Markdown format, so they can correctly archived in this repository. #
# Probably useful only to me, though it surely can be adapted to suit #
# the needs of different people. #
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
author="Pedro T. R. Pinheiro"
file=$1
if [ ! -f $file ]; then
echo "$file: No such file or directory."
exit 1
fi
if [ ! -r $file ]; then
echo "$file: Can't stat."
exit 2
fi
# Metadata
encoding=`file --mime-encoding ${file} | cut -d" " -f2`
lastMod=`stat -c %y ${file}`
presentDate=`date --rfc-3339=seconds`
title=`cat ${file} | head -1`
titleUnicode=`echo -ne ${title} | iconv -f ${encoding}`
# File contents
content=`cat ${file}`
contentUnicode=`echo -ne "${content}" | iconv -f ${encoding}`
# Lines can't be longer than 72 (-w 72), nor can
# words be broken (-s, only breaks on white space)
contentWraped=`echo "${contentUnicode}" | fold -w 72 -s`
yamlHeader=`cat << EOF
---
author: ${author}
title: ${titleUnicode}
date: "${lastMod}"
comment: "Original file written in ${encoding}, at ${lastMod}.
Converted to Markdown at ${presentDate}"
origDate: "${lastMod}"
convDate: "${presentDate}"
---
EOF`
output="${yamlHeader}\n\n${contentWraped}\n"
# Filename
# Not really useful to all people... Strong implies there's
# a ".txt" suffix to the filename.
baseName=`basename $file`
destFile=`echo $baseName | sed "s/.txt/.md/g"`
echo -ne "$output" > $destFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment