Skip to content

Commit 01661da

Browse files
add script to output a .csv of all existing guides in the library
1 parent 8761836 commit 01661da

File tree

4 files changed

+1968
-0
lines changed

4 files changed

+1968
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
# Get the absolute path to where the script is located
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
6+
# Define the path to parse, assuming it's always in "/docs/docs/guides" relative to the repository root
7+
GUIDES_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)/docs/guides"
8+
9+
# Define the output CSV file in the script's directory
10+
OUTPUT_FILE="$SCRIPT_DIR/urls.csv"
11+
12+
# Define the base URL
13+
BASE_URL="https://www.linode.com/docs/guides/"
14+
15+
# Initialize the CSV file with a header
16+
echo "URL,Title,Description,Keyword(s),Deprecation Status,Published Date,Updated Date" > "$OUTPUT_FILE"
17+
18+
# Function to extract and trim a given field from a line
19+
extract_field() {
20+
echo "$1" | sed "s/^$2:\s*//" | xargs
21+
}
22+
23+
# Function to sanitize the description field
24+
sanitize_description() {
25+
echo "$1" | sed 's/"/'\''/g; s/'"'"'/\\'"'"'/g; s/,/\\,/g; s/:/\\:/g; s/;/\\;/g' | xargs
26+
}
27+
28+
# Function to clean and format the keywords field
29+
format_keywords() {
30+
echo "$1" | sed 's/[][]//g' | sed 's/, */, /g' | xargs
31+
}
32+
33+
# Function to parse fields and build the CSV
34+
parse_directory_recursively() {
35+
local dir="$1"
36+
37+
# Find all index.md files recursively in the directory, excluding specified folders
38+
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
39+
# Initialize default values
40+
slug=""
41+
title=""
42+
description=""
43+
keywords=""
44+
deprecated="false"
45+
published_date=""
46+
updated_date=""
47+
48+
# Extract fields from each line
49+
while read -r line; do
50+
case "$line" in
51+
slug:*)
52+
slug=$(extract_field "$line" "slug")
53+
;;
54+
title:*)
55+
title=$(extract_field "$line" "title")
56+
;;
57+
description:*)
58+
description=$(extract_field "$line" "description")
59+
description=$(sanitize_description "$description")
60+
;;
61+
keywords:*)
62+
keywords=$(extract_field "$line" "keywords")
63+
keywords=$(format_keywords "$keywords")
64+
;;
65+
deprecated:*)
66+
deprecated_value=$(extract_field "$line" "deprecated")
67+
if [ "$deprecated_value" = "true" ]; then
68+
deprecated="true"
69+
fi
70+
;;
71+
published:*)
72+
published_date=$(extract_field "$line" "published")
73+
;;
74+
modified:*)
75+
updated_date=$(extract_field "$line" "modified")
76+
;;
77+
esac
78+
done < "$file"
79+
80+
# Construct the full URL without spaces, if slug exists
81+
if [ -n "$slug" ]; then
82+
full_url="${BASE_URL}${slug}"
83+
84+
# Append the data to the CSV file
85+
echo "\"$full_url\",\"$title\",\"$description\",\"$keywords\",\"$deprecated\",\"$published_date\",\"$updated_date\"" >> "$OUTPUT_FILE"
86+
fi
87+
done
88+
}
89+
90+
# Parse the designated guides directory
91+
parse_directory_recursively "$GUIDES_DIR"
92+
93+
echo "Data has been written to $OUTPUT_FILE"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function importCSVFromDrive() {
2+
// The name of the CSV file in Google Drive
3+
var fileName = 'urls.csv'; // Replace with your actual file name if different
4+
5+
try {
6+
// Fetch the file from Google Drive
7+
var files = DriveApp.getFilesByName(fileName);
8+
9+
if (files.hasNext()) {
10+
var file = files.next();
11+
var csvContent = file.getBlob().getDataAsString();
12+
13+
// Ensure that the CSV content is not empty
14+
if (!csvContent) {
15+
throw new Error('The CSV file is empty or could not be read.');
16+
}
17+
18+
// Parse the CSV content
19+
var csvData = Utilities.parseCsv(csvContent);
20+
21+
// Check if parsed data is non-empty
22+
if (csvData.length === 0) {
23+
throw new Error('Parsed CSV data is empty.');
24+
}
25+
26+
// Get the active sheet
27+
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
28+
29+
// Clear existing content on the sheet
30+
sheet.clear();
31+
32+
// Set the values to the sheet starting at A1
33+
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
34+
} else {
35+
throw new Error("File not found with the name: " + fileName);
36+
}
37+
} catch (e) {
38+
Logger.log('Failed to import CSV: ' + e.toString());
39+
SpreadsheetApp.getUi().alert('Failed to import the CSV file: ' + e.toString());
40+
}
41+
}

scripts/url-spreadsheet/test.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
URL,Title,Description,Keyword(s),Deprecation Status,Published Date,Updated Date
2+
https://example.com,"Sample Title","Sample Description","keyword1, keyword2",false,"2022-01-01","2022-02-01"

0 commit comments

Comments
 (0)