Skip to content

Commit

Permalink
Merge pull request #113 from felipealfonsog/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
felipealfonsog authored Oct 29, 2023
2 parents 9649612 + 15a43f8 commit d180bd3
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 52 deletions.
46 changes: 32 additions & 14 deletions src/termpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ def display_current_page(doc, current_page, total_pages, keyword=None):
else:
print(text)

def search_keyword(doc, total_pages, keyword):
found_pages = []

for page_num in range(total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
found_pages.append(page_num)

return found_pages

def display_search_results(doc, total_pages, keyword, start_page):
found_pages = search_keyword(doc, total_pages, keyword)

if not found_pages:
print(f"No more matches found for '{keyword}'.")
return start_page

for page_num in found_pages:
display_current_page(doc, page_num, total_pages, keyword)
choice = input("Press 'c' to continue searching, 'q' to exit, 'o' to open the page, 'r' to return to the start page: ")

if choice == 'q':
break
elif choice == 'o':
print(f"Opening page {page_num + 1}...")
# Placeholder for opening the page; implement your logic here
elif choice == 'r':
return start_page

return found_pages[-1] # Return the last found page

def display_pdf(pdf_filename):
try:
doc = fitz.open(pdf_filename)
Expand All @@ -93,20 +124,7 @@ def display_pdf(pdf_filename):
break
elif choice.lower() == 's':
keyword = input("Enter the keyword to search: ").lower()
found = False

for page_num in range(total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
display_current_page(doc, page_num, total_pages, keyword)
found = True

if not found:
print(f"No more matches found for '{keyword}'. Press 'q' to exit the search and return to the page where the search began. Press 'enter' to continue the search.")
exit_choice = input()
if exit_choice.lower() == 'q':
break

current_page = display_search_results(doc, total_pages, keyword, current_page)
except Exception as e:
print(f"Error: {e}")
finally:
Expand Down
83 changes: 59 additions & 24 deletions src/termpdf_stable.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ def display_current_page(doc, current_page, total_pages, keyword=None):
else:
print(text)

def search_keyword(doc, total_pages, keyword):
found_pages = []

for page_num in range(total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
found_pages.append(page_num)

return found_pages

def display_search_results(doc, total_pages, keyword, start_page):
found_pages = search_keyword(doc, total_pages, keyword)

if not found_pages:
print(f"No more matches found for '{keyword}'.")
return start_page

for page_num in found_pages:
display_current_page(doc, page_num, total_pages, keyword)
choice = input("Press 'c' to continue searching, 'q' to exit, 'o' to open the page, 'r' to return to the start page: ")

if choice == 'q':
break
elif choice == 'o':
print(f"Opening page {page_num + 1}...")
# Placeholder for opening the page; implement your logic here
elif choice == 'r':
return start_page

return found_pages[-1] # Return the last found page

def display_pdf(pdf_filename):
try:
doc = fitz.open(pdf_filename)
Expand All @@ -93,28 +124,7 @@ def display_pdf(pdf_filename):
break
elif choice.lower() == 's':
keyword = input("Enter the keyword to search: ").lower()
found = False

for page_num in range(current_page, total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
display_current_page(doc, page_num, total_pages, keyword)
found = True
break

if not found:
print(f"No matches found for '{keyword}'.")
else:
print("Search completed. Press 'q' to exit, 'Enter' to continue searching, or any other key to go back to the previous page.")

response = input()
if response.lower() == 'q':
return
elif response == '':
continue
else:
print("Invalid choice. Going back to the previous page.")

current_page = display_search_results(doc, total_pages, keyword, current_page)
except Exception as e:
print(f"Error: {e}")
finally:
Expand Down Expand Up @@ -159,6 +169,32 @@ def main():
"* Quit and return to the main menu.\n"
"* To exit the TermPDF Viewer, use 'q' in the main menu.\n"
"* To search within the PDF, use 's' during viewing and enter the keyword to search.\n"
"* \n"

"* Searching in PDF:\n"
"* \n"
"* Press 's' to initiate a search.\n"
"* Enter the keyword you want to search for when prompted.\n"
"* The matching words in the PDF will be highlighted in red and bold.\n"
"* Options:\n"
"* Press 'b' to go back to the previous page.\n"
"* Press 'f' to go forward to the next page.\n"
"* Press 'q' to exit the search and return to the page where the search began.\n"
"* Press 'enter' to continue the search.\n"
"* Exiting Search:\n"
"* \n"
"* If there are no more matches and you decide to exit the search ('q'), \n"
"* you will return to the page where the search began.\n"
"* You can choose to:\n"
"* Press 'enter' to continue the search.\n"
"* Press 'q' to exit the search and return to the page where the search began.\n"
"* Main Menu:\n"
"* \n"
"* Press '1' to scan for PDF files in the current directory.\n"
"* Press '2' to view scanned PDF files.\n"
"* Press '3' to quit the TermPDF Viewer.\n"
"* \n"

"-------------------------------------------------------------------------\n"
"* Important Notes:\n"
"* - The application has been tested on Linux and macOS.\n"
Expand Down Expand Up @@ -217,5 +253,4 @@ def main():

if __name__ == '__main__':
main()



46 changes: 32 additions & 14 deletions src/termpdf_unstable.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ def display_current_page(doc, current_page, total_pages, keyword=None):
else:
print(text)

def search_keyword(doc, total_pages, keyword):
found_pages = []

for page_num in range(total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
found_pages.append(page_num)

return found_pages

def display_search_results(doc, total_pages, keyword, start_page):
found_pages = search_keyword(doc, total_pages, keyword)

if not found_pages:
print(f"No more matches found for '{keyword}'.")
return start_page

for page_num in found_pages:
display_current_page(doc, page_num, total_pages, keyword)
choice = input("Press 'c' to continue searching, 'q' to exit, 'o' to open the page, 'r' to return to the start page: ")

if choice == 'q':
break
elif choice == 'o':
print(f"Opening page {page_num + 1}...")
# Placeholder for opening the page; implement your logic here
elif choice == 'r':
return start_page

return found_pages[-1] # Return the last found page

def display_pdf(pdf_filename):
try:
doc = fitz.open(pdf_filename)
Expand All @@ -93,20 +124,7 @@ def display_pdf(pdf_filename):
break
elif choice.lower() == 's':
keyword = input("Enter the keyword to search: ").lower()
found = False

for page_num in range(total_pages):
text = doc[page_num].get_text().lower()
if keyword in text:
display_current_page(doc, page_num, total_pages, keyword)
found = True

if not found:
print(f"No more matches found for '{keyword}'. Press 'q' to exit the search and return to the page where the search began. Press 'enter' to continue the search.")
exit_choice = input()
if exit_choice.lower() == 'q':
break

current_page = display_search_results(doc, total_pages, keyword, current_page)
except Exception as e:
print(f"Error: {e}")
finally:
Expand Down

0 comments on commit d180bd3

Please sign in to comment.