Skip to content

Commit

Permalink
Version 73. Added printCsv keyword to make it as easy to generate a C…
Browse files Browse the repository at this point in the history
…SV as it is to make an RSS feed.
  • Loading branch information
Breck Yunits authored and Breck Yunits committed Apr 3, 2024
1 parent c1c70c5 commit 57e67e8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ scrollNodePerf.cpuprofile
# Dump import examples here
tests/importExamples/
tests/kitchenSink/style.css
tests/kitchenSink/posts.csv

# Do not check in files built by Scroll
*.html
Expand Down
13 changes: 13 additions & 0 deletions grammar/csv.grammar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
printCsvParser
description Prints out a group as a CSV.
extends printFeedParser
example
printCsv index
permalink posts.csv
javascript
compile() {
const file = this.root.file
const files = file.getFilesInGroupsForEmbedding(this.getWordsFrom(1)).map(file => file.file)
const header = file.csvFields
return `${header.join(",")}\n${files.map(file => file.toCsv()).join("\n")}`
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scroll-cli",
"version": "72.4.4",
"version": "73.0.0",
"description": "Tools for thoughts.",
"main": "scroll.js",
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions releaseNotes.scroll
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title Scroll Release Notes

startColumns

# 73.0.0 4/03/2024
- 🎉 added `printCsv` keyword to make it as easy to generate a CSV of a blog as it is to generate an RSS feed

# 72.4.4 2/07/2024
- 🏥 do not print "undefined" in tables in rows missing columns

Expand Down
26 changes: 22 additions & 4 deletions scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const SVGS = {
email: `<svg viewBox="3 5 24 20" width="24" height="20" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(1, 0, 0, 1, 0, -289.0625)"><path style="opacity:1;stroke:none;stroke-width:0.49999997;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 5 5 C 4.2955948 5 3.6803238 5.3628126 3.3242188 5.9101562 L 14.292969 16.878906 C 14.696939 17.282876 15.303061 17.282876 15.707031 16.878906 L 26.675781 5.9101562 C 26.319676 5.3628126 25.704405 5 25 5 L 5 5 z M 3 8.4140625 L 3 23 C 3 24.108 3.892 25 5 25 L 25 25 C 26.108 25 27 24.108 27 23 L 27 8.4140625 L 17.121094 18.292969 C 15.958108 19.455959 14.041892 19.455959 12.878906 18.292969 L 3 8.4140625 z " transform="translate(0,289.0625)" id="rect4592"/></g></svg>`,
home: `<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12.7166 3.79541C12.2835 3.49716 11.7165 3.49716 11.2834 3.79541L4.14336 8.7121C3.81027 8.94146 3.60747 9.31108 3.59247 9.70797C3.54064 11.0799 3.4857 13.4824 3.63658 15.1877C3.7504 16.4742 4.05336 18.1747 4.29944 19.4256C4.41371 20.0066 4.91937 20.4284 5.52037 20.4284H8.84433C8.98594 20.4284 9.10074 20.3111 9.10074 20.1665V15.9754C9.10074 14.9627 9.90433 14.1417 10.8956 14.1417H13.4091C14.4004 14.1417 15.204 14.9627 15.204 15.9754V20.1665C15.204 20.3111 15.3188 20.4284 15.4604 20.4284H18.4796C19.0806 20.4284 19.5863 20.0066 19.7006 19.4256C19.9466 18.1747 20.2496 16.4742 20.3634 15.1877C20.5143 13.4824 20.4594 11.0799 20.4075 9.70797C20.3925 9.31108 20.1897 8.94146 19.8566 8.7121L12.7166 3.79541ZM10.4235 2.49217C11.3764 1.83602 12.6236 1.83602 13.5765 2.49217L20.7165 7.40886C21.4457 7.91098 21.9104 8.73651 21.9448 9.64736C21.9966 11.0178 22.0564 13.5119 21.8956 15.3292C21.7738 16.7067 21.4561 18.4786 21.2089 19.7353C20.9461 21.0711 19.7924 22.0001 18.4796 22.0001H15.4604C14.4691 22.0001 13.6655 21.1791 13.6655 20.1665V15.9754C13.6655 15.8307 13.5507 15.7134 13.4091 15.7134H10.8956C10.754 15.7134 10.6392 15.8307 10.6392 15.9754V20.1665C10.6392 21.1791 9.83561 22.0001 8.84433 22.0001H5.52037C4.20761 22.0001 3.05389 21.0711 2.79113 19.7353C2.54392 18.4786 2.22624 16.7067 2.10437 15.3292C1.94358 13.5119 2.00338 11.0178 2.05515 9.64736C2.08957 8.73652 2.55427 7.91098 3.28346 7.40886L10.4235 2.49217Z"/></svg>`
}
const CSV_FIELDS = ["date", "title", "permalink", "groups", "words"]

class ScrollFileSystem extends TreeFileSystem {
getScrollFile(filePath) {
Expand All @@ -76,6 +77,7 @@ class ScrollFileSystem extends TreeFileSystem {
}
}

const escapeCommas = str => (typeof str === "string" && str.includes(",") ? `"${str}"` : str)
const defaultScrollParser = new TreeFileSystem().getParser(Disk.getFiles(path.join(__dirname, "grammar")).filter(file => file.endsWith(GRAMMAR_EXTENSION)))
const DefaultScrollParser = defaultScrollParser.parser // todo: remove?

Expand All @@ -89,6 +91,7 @@ class ScrollFile {
this.filePath = absoluteFilePath
this.filename = path.basename(this.filePath)
this.folderPath = path.dirname(absoluteFilePath) + "/"
this.originalScrollCode = originalScrollCode

// PASS 1: IMPORT PASS
let afterImportPass = originalScrollCode
Expand Down Expand Up @@ -176,6 +179,15 @@ class ScrollFile {
return this.get(scrollKeywords.email)
}

get date() {
return dayjs(this.get(scrollKeywords.date) || 0).format(`MM/DD/YYYY`)
}

// Specifically this is the number of words in the source code for a post, not the # of words in the displayed post.
get words() {
return this.originalScrollCode.split(/\b/).length
}

get hasKeyboardNav() {
return this.scrollProgram.has(scrollKeywords.keyboardNav)
}
Expand Down Expand Up @@ -256,11 +268,11 @@ class ScrollFile {
}

get groups() {
return (this.scrollProgram.get(scrollKeywords.groups) || "").split(" ")
return this.scrollProgram.get(scrollKeywords.groups) || ""
}

get primaryGroup() {
return getGroup(this.groups[0], this.allFiles)
return getGroup(this.groups.split(" ")[0], this.allFiles)
}

getFilesInGroupsForEmbedding(groupNames) {
Expand Down Expand Up @@ -299,9 +311,9 @@ class ScrollFile {
if (!this._compiledStandalonePage) {
const { permalink } = this
const content = this.scrollProgram.compile().trim()
// Don't add html tags to XML/RSS feeds. A little hacky as calling a getter named _html_ to get _xml_ is not ideal. But
// Don't add html tags to XML/RSS/CSV feeds. A little hacky as calling a getter named _html_ to get _xml_ is not ideal. But
// <1% of use case so might be good enough.
const wrapWithHtmlTags = permalink.endsWith(".xml") || permalink.endsWith(".rss") ? false : true
const wrapWithHtmlTags = permalink.endsWith(".xml") || permalink.endsWith(".rss") || permalink.endsWith(".csv") ? false : true
this._compiledStandalonePage = wrapWithHtmlTags ? `<!DOCTYPE html><html lang="${this.lang}">` + content + "</html>" : content
}
return this._compiledStandalonePage
Expand All @@ -318,6 +330,12 @@ class ScrollFile {
return this.scrollProgram.get(scrollKeywords.baseUrl) ?? ""
}

csvFields = CSV_FIELDS

toCsv() {
return CSV_FIELDS.map(field => escapeCommas(this[field]))
}

toRss() {
const { title, canonicalLink } = this
return ` <item>
Expand Down
5 changes: 5 additions & 0 deletions tests/kitchenSink/csv.scroll
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import settings.scroll

permalink posts.csv

printCsv index
10 changes: 10 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ permalink feed.rss`)
areEqual(html.startsWith("<?xml "), true)
}

testTree.csv = areEqual => {
// Arrange
const page = new ScrollFile(`printCsv index
permalink posts.csv`)
// Act/Assert
const { html } = page
areEqual(html.startsWith("date,title,permalink,"), true)
console.log(html)
}

testTree.initCommand = async areEqual => {
const tempFolder = path.join(__dirname, "tempFolderForTesting")

Expand Down

0 comments on commit 57e67e8

Please sign in to comment.