diff --git a/printer.py b/printer.py index 625a7f8..084bc99 100755 --- a/printer.py +++ b/printer.py @@ -47,7 +47,7 @@ def insert_linebreaks(string, linelength): count += 1 return new -def printNow(query, results, results_count): +def printNow(query, results): if connected == True: printerpath = check_printer_path() try: @@ -58,31 +58,25 @@ def printNow(query, results, results_count): print(escpos['justify_center'], file=print_pointer, flush=True) printed = False - - if results_count == None or results_count == 'None': - results_count = 0 - results_count_max = results_count + 5 - current_count = 0 + results_count = 0 for x, document in results.items(): for n, sentence in enumerate(document['matches']): - if current_count == results_count: - printed = True - if results_count < results_count_max: - results_count += 1 - sentence = str(sentence) # Turn Flask Markup object back to normal python string - sentence = char_swap(sentence) - query = char_swap(query) - sentence = sentence.replace('\n', ' ').replace(' ', ' ') - pattern = r'[\s\W\_]'+query+r'[\s\W\_\n]|^'+query+'|'+query+'$' - match = re.search(pattern, sentence, flags=re.IGNORECASE) - if match: - match = match.group() - sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up - for i, part in enumerate(sentence_splitted): - if n == 0: # first sentence of this document - if i == 0: # start of a new sentence - print('', file=print_pointer, flush=True) - linebreak = ''' ** + if results_count < 4: + results_count += 1 + sentence = str(sentence) # Turn Flask Markup object back to normal python string + sentence = char_swap(sentence) + query = char_swap(query) + sentence = sentence.replace('\n', ' ').replace(' ', ' ') + pattern = r'[\s\W\_]'+query+r'[\s\W\_\n]|^'+query+'|'+query+'$' + match = re.search(pattern, sentence, flags=re.IGNORECASE) + if match: + match = match.group() + sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up + for i, part in enumerate(sentence_splitted): + if n == 0: # first sentence of this document + if i == 0: # start of a new sentence + print('', file=print_pointer, flush=True) + linebreak = ''' ** ** /** ** //** /** ** ************** @@ -90,23 +84,23 @@ def printNow(query, results, results_count): ** /** //** // /** // // ''' - print(linebreak, file=print_pointer, flush=True) # print line break ascii art - print('\n', file=print_pointer, flush=True) - title = insert_linebreaks(document['name'], 13) - print(escpos['largefont'], title, escpos['normalfont'], file=print_pointer, flush=True) # print the document name - print('\n', file=print_pointer, flush=True) + print(linebreak, file=print_pointer, flush=True) # print line break ascii art + print('\n', file=print_pointer, flush=True) + title = insert_linebreaks(document['name'], 13) + print(escpos['largefont'], title, escpos['normalfont'], file=print_pointer, flush=True) # print the document name + print('\n', file=print_pointer, flush=True) - # start of sentence - part = insert_linebreaks(part, 35) - print(part, file=print_pointer, flush=True) # print current part of sentence + # start of sentence + part = insert_linebreaks(part, 35) + print(part, file=print_pointer, flush=True) # print current part of sentence - if i + 1 != len(sentence_splitted): # if this part is not the last one - print(escpos['bold'], match, escpos['reset'], file=print_pointer, flush=True) # print query + if i + 1 != len(sentence_splitted): # if this part is not the last one + print(escpos['bold'], match, escpos['reset'], file=print_pointer, flush=True) # print query - # line breaker after each sentence - if i + 1 == len(sentence_splitted): - print('\n | \n', file=print_pointer, flush=True) - sleep(3) + # line breaker after each sentence + if i + 1 == len(sentence_splitted): + print('\n | \n', file=print_pointer, flush=True) + sleep(3) current_count += 1 @@ -124,12 +118,9 @@ def printNow(query, results, results_count): results_count = 0 # printNow(query, results, results_count) - return results_count - except: e = sys.exc_info()[0] print("Could not print: {}".format(e)) - return 0 # query = 'noise' # results, _, _ = readings.request_results(query) diff --git a/start.py b/start.py index 9ccda66..f3385c8 100644 --- a/start.py +++ b/start.py @@ -46,13 +46,6 @@ def index_lang(lang): connection = 'connected' else: connection = 'connected' - - # Check if print command - if request.args.get('results_count', ''): - results_count = int(request.args.get('results_count', '')) - print('results_count', results_count) - else: - results_count = 0 if request.args.get('q', ''): results, filenames, analytics = readings.request_results(query) @@ -60,9 +53,9 @@ def index_lang(lang): # print commands if request.args.get('print', '') == 'now': if connection == 'connected': - results_count = printer.printNow(query, results, results_count) + printer.printNow(query, results) - return flask.render_template(lang+'/results.html', query=query, results=results, filenames=filenames, connection=connection, suggestions=suggestions, analytics=analytics, lang=lang, results_count=results_count) + return flask.render_template(lang+'/results.html', query=query, results=results, filenames=filenames, connection=connection, suggestions=suggestions, analytics=analytics, lang=lang) else: index = readings.load_index() diff --git a/templates/en/results.html b/templates/en/results.html index 1da381d..f994afb 100644 --- a/templates/en/results.html +++ b/templates/en/results.html @@ -3,7 +3,7 @@ {% block nav %}