Skip to content

Instantly share code, notes, and snippets.

@rzmk
Last active January 22, 2024 02:28
Show Gist options
  • Save rzmk/e3d3f515b3fe59f5e88525d56e113e59 to your computer and use it in GitHub Desktop.
Save rzmk/e3d3f515b3fe59f5e88525d56e113e59 to your computer and use it in GitHub Desktop.
https://www.mueezkhan.com/blog/building-a-ckan-release-timeline. Converting CHANGELOG.rst from the https://github.com/ckan/ckan to CSV format with HTML descriptions for use with TimelineJS.
import csv
import markdown
# Generate a list from a file for importing into a CSV
def extract_data(file_path):
# Open the .rst file
with open(file_path, 'r') as file:
# Generate a string list of each line from the .rst file
lines = file.readlines()
# List of each release's data to import into CSV
data = []
# Row values for each column in the CSV
year = ''
month = ''
day = ''
headline = ''
text = ''
# Loop through each line in the .rst file
for line in lines:
# Remove starting and ending whitespaces (if any)
line = line.strip()
# Line starts with v or v. followed by a number (v2 or v.2)
if (line.startswith('v') and line[1].isdigit()) or (line.startswith('v.') and line[2].isdigit()):
# Append the accumulated data for the previous version to the list
data.append([year, month, day, headline, text])
# Reset the text for writing the current version's changes
text = ''
# Extract the release version and date information
headline = line.split()[0]
datetime = line.split()[1]
year, month, day = datetime.split('-')
# Ignore lines starting with '=' (section headers)
elif line.startswith('='):
continue
# Accumulate the lines between release versions into the text variable
else:
# Convert the current line to HTML
text += markdown.markdown(line) + '\n'
# Append the last release version's data to the data list
data.append([year, month, day, headline, text])
return data
# Write the extracted release data to a CSV file
def write_to_csv(data, output_file):
# Create or overwrite a CSV file
with open(output_file, 'w', newline='') as file:
# Instantiate a CSV file writer object
writer = csv.writer(file)
# Write the header row in the same format as the Google Sheet
writer.writerow(['Year', 'Month', 'Day', 'Headline', 'Text'])
# Write each release to the CSV file as a row
for row in data:
writer.writerow(row)
# Example usage
file_path = 'CHANGELOG.rst'
output_file = 'output.csv'
# Extract data from the .rst file
file_data = extract_data(file_path)
# Write the data to a .csv file
write_to_csv(file_data, output_file)
@rzmk
Copy link
Author

rzmk commented Jul 20, 2023

Note you may get errors with versions 0.2 and 0.1 since they don't have a day value for their release dates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment