Skip to content

Instantly share code, notes, and snippets.

@johnbumgarner
Last active October 5, 2022 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnbumgarner/5f7139b00bff22d9faa6a2badcc824fe to your computer and use it in GitHub Desktop.
Save johnbumgarner/5f7139b00bff22d9faa6a2badcc824fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
This Python script is designed to colorize specific words in a string of
text. The colorized text is written to an HTML file.
"""
__author__ = 'John Bumgarner'
__date__ = 'June 24, 2021'
__status__ = 'Production'
__license__ = 'MIT'
__copyright__ = "Copyright (C) 2021 John Bumgarner"
##################################################################################
# “AS-IS” Clause
#
# Except as represented in this agreement, all work produced by Developer is
# provided “AS IS”. Other than as provided in this agreement, Developer makes no
# other warranties, express or implied, and hereby disclaims all implied warranties,
# including any warranty of merchantability and warranty of fitness for a particular
# purpose.
##################################################################################
import random
import re as regex
def create_dynamic_regex(search_words):
"""
This function is used to create a dynamic regular expression
string and a list of random colors. Both these elements will
be used in the function colorize_text()
:param search_words: list of search terms
:return: regular expression search string and a list of colors
:rtype: string, list
"""
colors_required = create_list_of_colors(len(search_words))
number_of_search_words = len(search_words)
combined_string = ''
for search_word in search_words:
number_of_search_words -= 1
if number_of_search_words != 0:
current_string = ''.join(r'(\b' + search_word + r'\b)|')
combined_string = (combined_string + current_string)
elif number_of_search_words == 0:
current_string = ''.join(r'(\b' + search_word + r'\b)')
combined_string = (combined_string + current_string)
return combined_string, colors_required
def random_color():
"""
This function is used to create a random hexadecimal color.
:return: hexadecimal color
:rtype: string
"""
color = "#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)])
return color
def create_list_of_colors(number_of_colors):
"""
This function is used to generate a list of hexadecimal color,
which will be used in the function colorize_text()
:param number_of_colors:
:return: list of hexadecimal colors
:rtype: list
"""
list_of_colors = [random_color() for _ in range(number_of_colors)]
return list_of_colors
def colorize_text(text, regex_string, array_of_colors):
"""
This function is used to colorize specific words in a text string.
:param text: text string potentially containing specific words to colorize.
:param regex_string: regular expression search string
:param array_of_colors: list of hexadecimal colors
:return: colorized text
:rtype: string
"""
available_colors = array_of_colors
word_regex = regex.compile(f"{regex_string}", regex.IGNORECASE)
i = 0
html_output = ""
for word in word_regex.finditer(text):
html_output += "".join([text[i:word.start()],
"<strong><span style='color:%s'>" % available_colors[word.lastindex - 1],
text[word.start():word.end()], "</span></strong>"])
i = word.end()
return ''.join([html_output, text[word.end():]])
def create_html(text_to_search, words_to_find):
"""
This function is used generate colorized text that will
be outputting to an HTML file.
:param text_to_search: text string potentially containing specific words to colorize.
:param words_to_find: list of search terms.
:return: A HTML file containing colorized words.
:rtype: FileIO
"""
search_terms, colors = create_dynamic_regex(words_to_find)
colorize_html = colorize_text(text_to_search, search_terms, colors)
with open('colorize.html', 'w') as outfile:
outfile.write('\n\t<html>')
outfile.write('<body>\n')
outfile.write(f'<p style="font-family:verdana;font-size:18px">\n{colorize_html}\n</p>')
outfile.write('\n</body>')
outfile.write('\n\t</html>')
MLK_speech = """I have a dream that one day down in Alabama, with its vicious racists, with its governor having his
lips dripping with the words of interposition and nullification - one day right there in Alabama little black boys
and black girls will be able to join hands with little white boys and white girls as sisters and brothers.
I have a dream today. I have a dream that one day every valley shall be exalted, and every hill and mountain shall
be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the
Lord shall be revealed and all flesh shall see it together. This is our hope. This is the faith that I go back to
the South with. With this faith we will be able to hew out of the mountain of despair a stone of hope. With this faith
we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this
faith we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for
freedom together, knowing that we will be free one day. This will be the day, this will be the day when all of God's
children will be able to sing with new meaning "My country 'tis of thee, sweet land of liberty, of thee I sing.
Land where my father's died, land of the Pilgrim's pride, from every mountainside, let freedom ring!"""
words = ['I have a dream that one day', 'Alabama', 'god|lord|faith|pray', 'freedom']
create_html(MLK_speech, words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment