Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions scripts/url-spreadsheet/generate_urls_recursive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

# Get the absolute path to where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Define the path to parse, assuming it's always in "/docs/docs/guides" relative to the repository root
GUIDES_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)/docs/guides"

# Define the output CSV file in the script's directory
OUTPUT_FILE="$SCRIPT_DIR/urls.csv"

# Define the base URL
BASE_URL="https://www.linode.com/docs/guides/"

# Initialize the CSV file with a header
echo "URL,Title,Description,Keyword(s),Deprecation Status,Published Date,Updated Date" > "$OUTPUT_FILE"

# Function to extract and trim a given field from a line
extract_field() {
echo "$1" | sed "s/^$2:\s*//" | xargs
}

# Function to sanitize the description field
sanitize_description() {
echo "$1" | sed 's/"/'\''/g; s/'"'"'/\\'"'"'/g; s/,/\\,/g; s/:/\\:/g; s/;/\\;/g' | xargs
}

# Function to clean and format the keywords field
format_keywords() {
echo "$1" | sed 's/[][]//g' | sed 's/, */, /g' | xargs
}

# Function to parse fields and build the CSV
parse_directory_recursively() {
local dir="$1"

# Find all index.md files recursively in the directory, excluding specified folders
find "$dir" -type d \( -name "_shortguides" -o -name "concentrations" -o -name "audiences" -o -name "linode-writers-formatting-guide" \) -prune -o -type f -name "index.md" -print | while read -r file; do
# Initialize default values
slug=""
title=""
description=""
keywords=""
deprecated="false"
published_date=""
updated_date=""

# Extract fields from each line
while read -r line; do
case "$line" in
slug:*)
slug=$(extract_field "$line" "slug")
;;
title:*)
title=$(extract_field "$line" "title")
;;
description:*)
description=$(extract_field "$line" "description")
description=$(sanitize_description "$description")
;;
keywords:*)
keywords=$(extract_field "$line" "keywords")
keywords=$(format_keywords "$keywords")
;;
deprecated:*)
deprecated_value=$(extract_field "$line" "deprecated")
if [ "$deprecated_value" = "true" ]; then
deprecated="true"
fi
;;
published:*)
published_date=$(extract_field "$line" "published")
;;
modified:*)
updated_date=$(extract_field "$line" "modified")
;;
esac
done < "$file"

# Construct the full URL without spaces, if slug exists
if [ -n "$slug" ]; then
full_url="${BASE_URL}${slug}"

# Append the data to the CSV file
echo "\"$full_url\",\"$title\",\"$description\",\"$keywords\",\"$deprecated\",\"$published_date\",\"$updated_date\"" >> "$OUTPUT_FILE"
fi
done
}

# Parse the designated guides directory
parse_directory_recursively "$GUIDES_DIR"

echo "Data has been written to $OUTPUT_FILE"
41 changes: 41 additions & 0 deletions scripts/url-spreadsheet/grab-csv.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function importCSVFromDrive() {
// The name of the CSV file in Google Drive
var fileName = 'urls.csv'; // Replace with your actual file name if different

try {
// Fetch the file from Google Drive
var files = DriveApp.getFilesByName(fileName);

if (files.hasNext()) {
var file = files.next();
var csvContent = file.getBlob().getDataAsString();

// Ensure that the CSV content is not empty
if (!csvContent) {
throw new Error('The CSV file is empty or could not be read.');
}

// Parse the CSV content
var csvData = Utilities.parseCsv(csvContent);

// Check if parsed data is non-empty
if (csvData.length === 0) {
throw new Error('Parsed CSV data is empty.');
}

// Get the active sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

// Clear existing content on the sheet
sheet.clear();

// Set the values to the sheet starting at A1
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
} else {
throw new Error("File not found with the name: " + fileName);
}
} catch (e) {
Logger.log('Failed to import CSV: ' + e.toString());
SpreadsheetApp.getUi().alert('Failed to import the CSV file: ' + e.toString());
}
}
2 changes: 2 additions & 0 deletions scripts/url-spreadsheet/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
URL,Title,Description,Keyword(s),Deprecation Status,Published Date,Updated Date
https://example.com,"Sample Title","Sample Description","keyword1, keyword2",false,"2022-01-01","2022-02-01"
Loading