''' 2021-05-22 To Dead Code Previously Entitled: utilities_text_refactored Created on Mar 7, 2019 @author: Brett Paufler Copyright Brett Paufler Helper Functions for webpage_ scripts ''' def label_text(text_in): '''Returns labelled tuple from input_text using Bretts Custom iPhone Mark Up, using head_n syntax. See the elifs (below) for labelling schema title: first line of text head_n: section headers magazine, books, titles, tag lines entry: if ';' in line comment: if '{' starts line {} might come to mean italics someday ''' #Split into Lines lines = text_in.split('\n') lines = [line.strip() for line in lines] #First Line of Email is Always the Title title = lines.pop(0) labelled = [('title', title)] head_count = 0 for line in lines: if line.startswith('{'): line = line.replace('{', '') line = line.replace('}', '') line = line.strip() labelled.append(('comment', line)) head_count = 0 elif ';' in line: labelled.append(('entry', line)) head_count = 0 else: labelled.append( ('head_%d' % head_count, line)) head_count += 1 return labelled def to_sentence(text_in): '''Capitalizes first letter and adds default period. Takes into account quotes, questions, eclamaitions.''' #Capitalize the first letter #print text_in if text_in[0] in ['"', "'"]: c_text = "%s%s%s" % ( text_in[0], text_in[1].upper(), text_in[2:]) else: c_text = "%s%s" % ( text_in[0].upper(), text_in[1:]) #Add trailing period if c_text.endswith('ellipsis') or c_text.endswith('ellipses'): text_out = c_text[:-9] + '...' elif c_text[-1] in '.?!': text_out = c_text elif c_text[-1] in ['"', "'"]: if c_text[-2] in '.?!': text_out = c_text else: text_out = "%s%s%s" % ( c_text[:-1], '.', c_text[-1]) elif (c_text[-4:] == '' and c_text[-5] in '.?!'): text_out = c_text else: text_out = c_text + '.' return text_out def three_head_label_to_html( labeled_list, page_title_template, comment_template, book_title_template, book_tag_template, author_template): '''Parses a three head labelled list into html body text_line using passed templates Used for Ten Books''' text_out = '' for num, (item_label, item_text) in enumerate(labeled_list): text_line = '' #Parser if item_label == 'title': if page_title_template: text_line = page_title_template % item_text elif item_label == 'comment': text_line = comment_template % item_text elif item_label == 'head_0': text_line = book_title_template % item_text elif item_label == 'head_1': if labeled_list[num + 1][0] == 'head_2': text_line = book_tag_template % item_text else: text_line = author_template % item_text #Handles ALl extra cases elif 'head_' in item_label: text_line = author_template % item_text elif item_label == 'entry': text_line = semi_ellipsis_to_htlm_list(item_text) else: print '%s: This Label Not Handled' % item_label print num, item_label, item_text assert True == False text_out += text_line return text_out def semi_ellipsis_to_htlm_list(text_in): text_out = '\n' return text_out def escape_amp(text): '''Normalize & as the default &''' text = text.replace('&', 'andand') text = text.replace('&', 'andand') text = text.replace('andand', '&') text = text.replace('©', '&#copy;') text = text.replace('©', '©') return text def escape_ellipses(text): '''ellipsis and the like replaced as ...''' text = text.replace('ellipses', '...') text = text.replace('ellipse', '...') text = text.replace('Ellipses', '...') text = text.replace('Ellipse', '...') text = text.replace('....', '...') return text def escape_sequences(text): text = escape_amp(text) text = escape_ellipses(text) return text