txt2tags - verbatim bold
txt2tags is an absolutely gem of a tool and I have described it in a previous primer here. One thing that annoys me a little is the Verbatim block. While I understand that the verbatium block is to ignore markdowns there is times I like to bold bits within the block. Take for example:
~$ sudo mysql -u root -p
  Enter password: mypassword
  Welcome to the MariaDB monitor.  Commands end with ; or \g.
  Your MariaDB connection id is 228
  Server version: 10.0.28-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
  
  Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
  MariaDB [(none)]> SHOW DATABASES;
  +--------------------+
  | Database           |
  +--------------------+
  | my_database        |
  | performance_schema |
  +--------------------+
  2 rows in set (0.00 sec)
Now wouldn't it be nice if the commands and the password could be bolded for emphasis. Well I think so and I have written a short program to deal with the problem. The output looks like this.
~$ sudo mysql -u root -p
  Enter password: mypassword
  Welcome to the MariaDB monitor.  Commands end with ; or \g.
  Your MariaDB connection id is 228
  Server version: 10.0.28-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
  Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  MariaDB [(none)]> SHOW DATABASES;
  +--------------------+
  | Database           |
  +--------------------+
  | my_database        |
  | performance_schema |
  +--------------------+
  2 rows in set (0.00 sec)
When writing the t2t file use an additional markdown (£££) around elements within verbatim blocks you would like bolded in the final html file. Put the txt2tags-verbatim-bold-£££.py file in the same directory. Convert the .t2t file to .html in the normal way. Then apply the txt2tags-verbatim-bold-£££.py to the newly created html file. It will create a new html file with the £££ tags removed and replaced with the html bold tags. In the example below filename.t2t is a txt2tags file which has £££ tags surrounding text within verbatim blocks to be bolded in the final output.
~ $ txt2tags --target=html filename.t2t
txt2tags wrote filename.html
~ $ ./txt2tags-verbatim-bold-£££.py filename.html 
Created new file filename-bold.html
Here is the source code for the python script.
~ $ cat txt2tags-verbatim-bold-£££.py
#! /usr/bin/env python3
"""
txt2tags-verbatim-bold-£££.py: Replaces £££ tags in verbatim sections of t2tags outputted html
This program is applied to the .html file not the .t2t file.
~ $ txt2tags --target=html filename.t2t
txt2tags wrote filename.html
~ $ ./txt2tags-verbatim-bold-£££.py filename.html
Created new file filename-bold.html
"""
__author__      = "Diarmuid O'Briain"
__copyright__   = "Copyright 2016, Diarmuid O'Briain"
__license__     = "https://www.gnu.org/licenses/gpl.txt"
import re
import os.path
import sys
# Check for file given as shell command argument
if len(sys.argv) < 2:
   print ("No file given.")
   print ("\n Usage", sys.argv[0], "<filename.html>\n")  
   sys.exit()  
elif (os.path.isfile(sys.argv[1])):
    infile = sys.argv[1]
else:
   print ("The file", sys.argv[1], "does not exist.")
   print ("\n Usage", sys.argv[0], "<filename.html>\n")
   sys.exit()
# Extract main part of filename, dropping the .html extension
matched = re.findall(r'(.*?).html', infile)
# add -bold.html to the matched main part of filename
outfile = "{}-bold.html".format(matched[0])
# Check for existance of outfile and if exists delete
if (os.path.isfile(outfile)):
    os.remove(outfile)
    print ("Removing old", outfile)
    
# Read infile line by line, find lines with '£££'
infh = open(infile, mode='r')
outfh = open(outfile, mode='w', encoding='utf-8')
for line in infh:
    if (re.search('£££(.*?)£££', line)):
        bolded = re.split("£££", line)
        outfh.write(bolded[0]+'<B>'+bolded[1]+'</B>'+bolded[2])
    else:
        outfh.write(line)
# Note to terminal of new outfile 
print("Created new file", outfile)
# Close filehandle
infh.close()
outfh.close()
# End

