From 599231ef32dcc529e82d3e019a5c938e789193f7 Mon Sep 17 00:00:00 2001 From: prppedro Date: Sat, 22 Jan 2022 14:48:52 -0300 Subject: [PATCH] =?UTF-8?q?Pequeno=20utilit=C3=A1rio=20que=20converte=20os?= =?UTF-8?q?=20txt's=20antigos=20da=20minha=20era=20Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/txt2md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 tools/txt2md diff --git a/tools/txt2md b/tools/txt2md new file mode 100755 index 0000000..6a7fa7d --- /dev/null +++ b/tools/txt2md @@ -0,0 +1,66 @@ +#!/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