Last active
July 17, 2023 12:43
-
-
Save maxdemaio/9963fd7a68f6fac3c18c22d83a2a3048 to your computer and use it in GitHub Desktop.
Generate the Reading Time of a Markdown File/Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import re | |
average_reading_speed = 200 # Adjust as needed | |
# Step 1: Get the path from command line argument | |
if len(sys.argv) < 2: | |
print("Please provide the file or directory path as a command line argument.") | |
sys.exit(1) | |
path = sys.argv[1] | |
# Step 2: Process the path | |
if os.path.isfile(path): | |
# Single file | |
file_paths = [path] | |
elif os.path.isdir(path): | |
# Directory | |
file_paths = [ | |
os.path.join(path, file) | |
for file in os.listdir(path) | |
if file.lower().endswith('.md') | |
] | |
else: | |
print("Invalid file or directory path.") | |
sys.exit(1) | |
# Step 3-5: Loop through files and calculate reading time | |
for file_path in file_paths: | |
# Read the Markdown file | |
try: | |
with open(file_path, 'r', encoding='utf-8') as file: | |
markdown_content = file.read() | |
except FileNotFoundError: | |
print(f"File not found: {file_path}") | |
continue | |
# Convert Markdown to plain text | |
plain_text = re.sub(r'#.*$', '', markdown_content, flags=re.MULTILINE) # Remove headings | |
plain_text = re.sub(r'!\[.*?\]\(.*?\)', '', plain_text) # Remove images | |
plain_text = re.sub(r'<.*?>', '', plain_text) # Remove HTML tags | |
plain_text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', plain_text) # Replace links with link text | |
# Calculate the word count | |
word_count = len(re.findall(r'\w+', plain_text)) | |
# Calculate the reading time | |
reading_time = int(word_count / average_reading_speed) + 1 | |
# Print the estimated reading time | |
print(f"File: {file_path}") | |
print(f"Estimated reading time: {reading_time} minutes\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment