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