-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
scroll.js
executable file
·1133 lines (980 loc) · 40.1 KB
/
scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env node
// NPM ecosystem includes
const parseArgs = require("minimist")
const path = require("path")
const fs = require("fs")
const lodash = require("lodash")
const dayjs = require("dayjs")
// Particles Includes
const { Particle } = require("scrollsdk/products/Particle.js")
const { Disk } = require("scrollsdk/products/Disk.node.js")
const { Utils } = require("scrollsdk/products/Utils.js")
const { ParticleFileSystem } = require("scrollsdk/products/ParticleFileSystem.js")
const stumpParser = require("scrollsdk/products/stump.nodejs.js")
const packageJson = require("./package.json")
// Constants
const SCROLL_VERSION = packageJson.version
const SCROLL_FILE_EXTENSION = ".scroll"
const PARSERS_FILE_EXTENSION = ".parsers"
const importParticleRegex = /^(import .+|[a-zA-Z\_\-\.0-9\/]+\.(scroll|parsers)$)/gm
// Todo: how to keep in sync with scroll files?
const scrollKeywords = {
title: "title",
description: "description",
viewSourceUrl: "viewSourceUrl",
permalink: "permalink",
canonicalUrl: "canonicalUrl",
image: "image",
date: "date",
endSnippet: "endSnippet",
tags: "tags",
keyboardNav: "keyboardNav",
replace: "replace",
replaceJs: "replaceJs",
replaceNodejs: "replaceNodejs",
buildConcepts: "buildConcepts",
buildMeasures: "buildMeasures",
buildTxt: "buildTxt",
buildHtml: "buildHtml",
buildPdf: "buildPdf",
buildRss: "buildRss",
import: "import",
importOnly: "importOnly",
baseUrl: "baseUrl",
viewSourceBaseUrl: "viewSourceBaseUrl",
openGraphImage: "openGraphImage",
openGraph: "openGraph",
email: "email",
rssFeedUrl: "rssFeedUrl"
}
const SVGS = {
git: `<svg xmlns="http://www.w3.org/2000/svg" width="92pt" height="92pt" viewBox="0 0 92 92"><path d="M90.156 41.965 50.036 1.848a5.913 5.913 0 0 0-8.368 0l-8.332 8.332 10.566 10.566a7.03 7.03 0 0 1 7.23 1.684 7.043 7.043 0 0 1 1.673 7.277l10.183 10.184a7.026 7.026 0 0 1 7.278 1.672 7.04 7.04 0 0 1 0 9.957 7.045 7.045 0 0 1-9.961 0 7.038 7.038 0 0 1-1.532-7.66l-9.5-9.497V59.36a7.04 7.04 0 0 1 1.86 11.29 7.04 7.04 0 0 1-9.957 0 7.04 7.04 0 0 1 0-9.958 7.034 7.034 0 0 1 2.308-1.539V33.926a7.001 7.001 0 0 1-2.308-1.535 7.049 7.049 0 0 1-1.516-7.7L29.242 14.273 1.734 41.777a5.918 5.918 0 0 0 0 8.371L41.855 90.27a5.92 5.92 0 0 0 8.368 0l39.933-39.934a5.925 5.925 0 0 0 0-8.371"/></svg>`
}
const CSV_FIELDS = ["date", "year", "title", "permalink", "authors", "tags", "wordCount", "minutes"]
const unCamelCase = str => str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/^./, match => match.toUpperCase())
const makeLodashOrderByParams = str => {
const part1 = str.split(" ")
const part2 = part1.map(col => (col.startsWith("-") ? "desc" : "asc"))
return [part1.map(col => col.replace(/^\-/, "")), part2]
}
class ScrollFileSystem extends ParticleFileSystem {
getScrollFile(filePath) {
return this._getParsedFile(filePath, ScrollFile)
}
productCache = {}
writeProduct(absolutePath, content) {
this.productCache[absolutePath] = content
return this.write(absolutePath, content)
}
parsedFiles = {}
_getParsedFile(absolutePath, parser) {
if (this.parsedFiles[absolutePath]) return this.parsedFiles[absolutePath]
this.parsedFiles[absolutePath] = new parser(undefined, absolutePath, this)
return this.parsedFiles[absolutePath]
}
folderCache = {}
getScrollFilesInFolder(folderPath) {
return this._getFilesInFolder(folderPath, SCROLL_FILE_EXTENSION)
}
getParserFilesInFolder(folderPath) {
return this._getFilesInFolder(folderPath, PARSERS_FILE_EXTENSION)
}
_getFilesInFolder(folderPath, extension) {
folderPath = Utils.ensureFolderEndsInSlash(folderPath)
if (this.folderCache[folderPath]) return this.folderCache[folderPath]
const files = this.list(folderPath)
.filter(file => file.endsWith(extension))
.map(filePath => this.getScrollFile(filePath))
const sorted = lodash.sortBy(files, file => file.timestamp).reverse()
sorted.forEach((file, index) => (file.timeIndex = index))
this.folderCache[folderPath] = sorted
return this.folderCache[folderPath]
}
}
const removeBlanks = data => data.map(obj => Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== "")))
const escapeCommas = str => (typeof str === "string" && str.includes(",") ? `"${str}"` : str)
const defaultScrollParser = new ParticleFileSystem().getParser(Disk.getFiles(path.join(__dirname, "parsers")).filter(file => file.endsWith(PARSERS_FILE_EXTENSION)))
const DefaultScrollParser = defaultScrollParser.parser // todo: remove?
// todo: tags is currently matching partial substrings
const getFilesWithTag = (tag, files) => files.filter(file => file.buildsHtml && file.tags.includes(tag))
// todo: clean this up
const getCruxAtoms = rootParserProgram =>
rootParserProgram
.filter(particle => particle.getLine().endsWith("Parser") && !particle.getLine().startsWith("abstract"))
.map(particle => particle.get("crux") || particle.getLine())
.map(line => line.replace("Parser", ""))
const measureCache = new Map()
const parseMeasures = parser => {
if (measureCache.get(parser)) return measureCache.get(parser)
// Generate a fake program with one of every of the available parsers. Then parse it. Then we can easily access the meta data on the parsers
const dummyProgram = new parser(
Array.from(
new Set(
getCruxAtoms(parser.cachedHandParsersProgramRoot) // is there a better method name than this?
)
).join("\n")
)
// Delete any particles that are not measures
dummyProgram.filter(particle => !particle.isMeasure).forEach(particle => particle.destroy())
dummyProgram.forEach(particle => {
// add nested measures
Object.keys(particle.definition.firstAtomMapWithDefinitions).forEach(key => particle.appendLine(key))
})
// Delete any nested particles that are not measures
dummyProgram.topDownArray.filter(particle => !particle.isMeasure).forEach(particle => particle.destroy())
const measures = dummyProgram.topDownArray.map(particle => {
return {
Name: particle.measureName,
Values: 0,
Coverage: 0,
Question: particle.definition.description,
Example: particle.definition.getParticle("example")?.subparticlesToString() || "",
Type: particle.typeForWebForms,
Source: particle.sourceDomain,
//Definition: parsedProgram.root.file.filename + ":" + particle.lineNumber
SortIndex: particle.sortIndex,
IsComputed: particle.isComputed,
IsRequired: particle.isMeasureRequired,
IsConceptDelimiter: particle.isConceptDelimiter,
Crux: particle.definition.get("crux")
}
})
measureCache.set(parser, lodash.sortBy(measures, "SortIndex"))
return measureCache.get(parser)
}
const getConcepts = parsed => {
const concepts = []
let currentConcept
parsed.forEach(particle => {
if (particle.isConceptDelimiter) {
if (currentConcept) concepts.push(currentConcept)
currentConcept = []
}
if (currentConcept && particle.isMeasure) currentConcept.push(particle)
})
if (currentConcept) concepts.push(currentConcept)
return concepts
}
const addMeasureStats = (concepts, measures) =>
measures.map(measure => {
let Type = false
concepts.forEach(concept => {
const value = concept[measure.Name]
if (value === undefined || value === "") return
measure.Values++
if (!Type) {
measure.Example = value.toString().replace(/\n/g, " ")
measure.Type = typeof value
Type = true
}
})
measure.Coverage = Math.floor((100 * measure.Values) / concepts.length) + "%"
return measure
})
// note that this is currently global, assuming there wont be
// name conflicts in computed measures in a single scroll
const measureFnCache = {}
const computeMeasure = (parsedProgram, measureName, concept, concepts) => {
if (!measureFnCache[measureName]) {
// a bit hacky but works??
const particle = parsedProgram.appendLine(measureName)
measureFnCache[measureName] = particle.computeValue
particle.destroy()
}
return measureFnCache[measureName](concept, measureName, parsedProgram, concepts)
}
const parseConcepts = (parsedProgram, measures) => {
// Todo: might be a perf/memory/simplicity win to have a "segment" method in ScrollSDK, where you could
// virtually split a Particle into multiple segments, and then query on those segments.
// So we would "segment" on "id ", and then not need to create a bunch of new objects, and the original
// already parsed lines could then learn about/access to their respective segments.
const conceptDelimiter = measures.filter(measure => measure.IsConceptDelimiter)[0]
if (!conceptDelimiter) return []
const concepts = parsedProgram.split(conceptDelimiter.Crux || conceptDelimiter.Name)
concepts.shift() // Remove the part before "id"
return concepts.map(concept => {
const row = {}
measures.forEach(measure => {
const measureName = measure.Name
const measureKey = measure.Crux || measureName.replace(/_/g, " ")
if (!measure.IsComputed) row[measureName] = concept.getParticle(measureKey)?.measureValue ?? ""
else row[measureName] = computeMeasure(parsedProgram, measureName, concept, concepts)
})
return row
})
}
const arrayToCSV = (data, delimiter = ",") => {
if (!data.length) return ""
// Extract headers
const headers = Object.keys(data[0])
const csv = data.map(row =>
headers
.map(fieldName => {
const fieldValue = row[fieldName]
// Escape commas if the value is a string
if (typeof fieldValue === "string" && fieldValue.includes(delimiter)) {
return `"${fieldValue.replace(/"/g, '""')}"` // Escape double quotes and wrap in double quotes
}
return fieldValue
})
.join(delimiter)
)
csv.unshift(headers.join(delimiter)) // Add header row at the top
return csv.join("\n")
}
class ScrollFile {
constructor(codeAtStart, absoluteFilePath = "", fileSystem = new ScrollFileSystem({})) {
this.fileSystem = fileSystem
if (codeAtStart === undefined) codeAtStart = absoluteFilePath ? fileSystem.read(absoluteFilePath) : ""
this.filePath = absoluteFilePath
this.filename = path.basename(this.filePath)
this.filenameNoExtension = this.filename.replace(SCROLL_FILE_EXTENSION, "")
this.folderPath = path.dirname(absoluteFilePath) + "/"
// PASS 1: READ FULL FILE
this.codeAtStart = codeAtStart
// PASS 2: READ AND REPLACE IMPORTs
let codeAfterImportPass = codeAtStart
let parser = DefaultScrollParser
if (absoluteFilePath) {
const assembledFile = fileSystem.assembleFile(absoluteFilePath, defaultScrollParser.parsersCode)
this.importOnly = assembledFile.isImportOnly
codeAfterImportPass = assembledFile.afterImportPass
if (assembledFile.parser) parser = assembledFile.parser
}
this.codeAfterImportPass = codeAfterImportPass
// PASS 3: READ AND REPLACE MACROS. PARSE AND REMOVE MACROS DEFINITIONS THEN REPLACE REFERENCES.
const codeAfterMacroPass = this.evalMacros(codeAfterImportPass, codeAtStart, this.filePath)
// PASS 4: READ WITH STD COMPILER OR CUSTOM COMPILER.
this.codeAfterMacroPass = codeAfterMacroPass
this.parser = parser
this.scrollProgram = new parser(codeAfterMacroPass)
this.scrollProgram.setFile(this)
this.timestamp = dayjs(this.scrollProgram.get(scrollKeywords.date) ?? fileSystem.getCTime(absoluteFilePath) ?? 0).unix()
this.permalink = this.scrollProgram.get(scrollKeywords.permalink) || (this.filename ? this.filenameNoExtension + ".html" : "")
}
// todo: speed this up and do a proper release. also could add more metrics like this.
get lastCommitTime() {
if (this._lastCommitTime === undefined) {
try {
this._lastCommitTime = require("child_process").execSync(`git log -1 --format="%at" -- "${this.filePath}"`).toString().trim()
} catch (err) {
this._lastCommitTime = 0
}
}
return this._lastCommitTime
}
formatAndSave() {
const { codeAtStart, formatted } = this
if (codeAtStart === formatted) return false
this.fileSystem.write(this.filePath, formatted)
return true
}
get absoluteLink() {
return this.ensureAbsoluteLink(this.permalink)
}
getFileFromId(id) {
return this.fileSystem.getScrollFile(path.join(this.folderPath, id + ".scroll"))
}
get allScrollFiles() {
return this.fileSystem.getScrollFilesInFolder(this.folderPath)
}
toSearchTsvRow(relativePath = "") {
const text = this.asTxt.replace(/(\t|\n)/g, " ").replace(/</g, "<")
return [this.title, relativePath + this.permalink, text, this.date, this.wordCount, this.minutes].join("\t")
}
// todo: clean up this naming pattern and add a parser instead of special casing 404.html
get allHtmlFiles() {
return this.allScrollFiles.filter(file => file.buildsHtml && file.permalink !== "404.html")
}
get buildsHtml() {
const { permalink } = this
return !this.importOnly && (permalink.endsWith(".html") || permalink.endsWith(".htm"))
}
get parserIds() {
return this.scrollProgram.topDownArray.map(particle => particle.definition.id)
}
_concepts
get concepts() {
if (this._concepts) return this._concepts
this._concepts = parseConcepts(this.scrollProgram, this.measures)
return this._concepts
}
_measures
get measures() {
if (this._measures) return this._measures
this._measures = parseMeasures(this.parser)
return this._measures
}
get parsersBundle() {
let code =
`parsers/atomTypes.parsers
parsers/root.parsers
parsers/build.parsers
parsers/comments.parsers
parsers/blankLine.parsers
parsers/measures.parsers
parsers/errors.parsers`
.trim()
.split("\n")
.map(filepath => Disk.read(path.join(__dirname, filepath)))
.join("\n\n")
.replace("catchAllParser catchAllParagraphParser", "catchAllParser errorParser") + this.scrollProgram.toString()
code = code.replace(/^importOnly\n/gm, "").replace(importParticleRegex, "")
code = new Particle(code)
code.getParticle("commentParser").appendLine("boolean suggestInAutocomplete false")
code.getParticle("slashCommentParser").appendLine("boolean suggestInAutocomplete false")
code.getParticle("buildMeasuresParser").appendLine("boolean suggestInAutocomplete false")
return code.toString()
}
get tables() {
return this.scrollProgram.filter(particle => particle.isTabularData && particle.isFirst)
}
_formatConcepts(parsed) {
const concepts = getConcepts(parsed)
if (!concepts.length) return false
// does a destructive sort in place on the parsed program
concepts.forEach(concept => {
let currentSection
const newCode = lodash
.sortBy(concept, ["sortIndex"])
.map(particle => {
let newLines = ""
const section = particle.sortIndex.toString().split(".")[0]
if (section !== currentSection) {
currentSection = section
newLines = "\n"
}
return newLines + particle.toString()
})
.join("\n")
concept.forEach((particle, index) => (index ? particle.destroy() : ""))
concept[0].replaceParticle(() => newCode)
})
}
get formatted() {
// Todo: think this through and create the best long term strategy. Perhaps sortIndex float is a universal property on Parsers.
/* Current layout:
[importOnly?]
[topMatter*]
[measurements*]
[content*] */
let formatted = this.codeAtStart
const parsed = new this.parser(formatted)
let topMatter = []
let importOnly = ""
parsed
.filter(particle => particle.isTopMatter)
.forEach(particle => {
if (particle.getLine() === scrollKeywords.importOnly) {
importOnly = particle.toString() + "\n" // Put importOnly first, if present
return particle.destroy()
}
topMatter.push(particle.toString())
particle.destroy()
})
this._formatConcepts(parsed)
let topMatterThenContent = importOnly + topMatter.sort().join("\n").trim() + "\n\n" + parsed.toString().trim()
return (
topMatterThenContent
.trim() // Remove leading whitespace
.replace(/(\S.*?)[ \t]*$/gm, "$1") // Trim trailing whitespace, except for lines that are *all* whitespace (in which case the whitespace may be semantic particles)
.replace(/\n\n\n+/g, "\n\n") // Maximum 2 newlines in a row
.replace(/\n+$/, "") + "\n"
) // End Scroll files in a newline character POSIX style for better working with tools like git
}
_compileArray(filename, arr) {
const parts = filename.split(".")
const format = parts.pop()
if (format === "json") return JSON.stringify(removeBlanks(arr), null, 2)
if (format === "js") return `const ${parts[0]} = ` + JSON.stringify(removeBlanks(arr), null, 2)
if (format === "csv") return arrayToCSV(arr)
if (format === "tsv") return arrayToCSV(arr, "\t")
if (format === "particles") return particles.toString()
return particles.toString()
}
compileConcepts(filename = "csv", sortBy = "") {
if (!sortBy) return this._compileArray(filename, this.concepts)
const orderBy = makeLodashOrderByParams(sortBy)
return this._compileArray(filename, lodash.orderBy(this.concepts, orderBy[0], orderBy[1]))
}
_withStats
get measuresWithStats() {
if (!this._withStats) this._withStats = addMeasureStats(this.concepts, this.measures)
return this._withStats
}
compileMeasures(filename = "csv", sortBy = "") {
const withStats = this.measuresWithStats
if (!sortBy) return this._compileArray(filename, withStats)
const orderBy = makeLodashOrderByParams(sortBy)
return this._compileArray(filename, lodash.orderBy(withStats, orderBy[0], orderBy[1]))
}
evalMacros(code, codeAtStart, absolutePath) {
// note: the 2 params above are not used in this method, but may be used in user eval code. (todo: cleanup)
const regex = /^replace/gm
if (!regex.test(code)) return code
const particle = new Particle(code) // todo: this can be faster. a more lightweight particle class?
// Process macros
const macroMap = {}
particle
.filter(particle => {
const parserAtom = particle.firstAtom
return parserAtom === scrollKeywords.replace || parserAtom === scrollKeywords.replaceJs || parserAtom === scrollKeywords.replaceNodejs
})
.forEach(particle => {
let value = particle.length ? particle.subparticlesToString() : particle.getAtomsFrom(2).join(" ")
const kind = particle.firstAtom
if (kind === scrollKeywords.replaceJs) value = eval(value)
if (kind === scrollKeywords.replaceNodejs) {
const tempPath = this.filePath + ".js"
if (Disk.exists(tempPath)) throw new Error(`Failed to write/require replaceNodejs snippet since '${tempPath}' already exists.`)
try {
Disk.write(tempPath, value)
const results = require(tempPath)
Object.keys(results).forEach(key => (macroMap[key] = results[key]))
} catch (err) {
console.error(`Error in evalMacros in file '${this.filePath}'`)
console.error(err)
} finally {
Disk.rm(tempPath)
}
} else macroMap[particle.getAtom(1)] = value
particle.destroy() // Destroy definitions after eval
})
const keys = Object.keys(macroMap)
if (!keys.length) return code
let codeAfterMacroSubstitution = particle.asString
// Todo: speed up. build a template?
Object.keys(macroMap).forEach(key => (codeAfterMacroSubstitution = codeAfterMacroSubstitution.replace(new RegExp(key, "g"), macroMap[key])))
return codeAfterMacroSubstitution
}
get mtimeMs() {
return this.fileSystem.getMTime(this.filePath)
}
SVGS = SVGS
SCROLL_VERSION = SCROLL_VERSION
importOnly = false
filePath = ""
compileStumpCode(code) {
return new stumpParser(code).compile()
}
log(message) {}
get date() {
const date = this.get(scrollKeywords.date) || this.timestamp * 1000 || 0
return dayjs(date).format(`MM/DD/YYYY`)
}
get year() {
return dayjs(this.date).format(`YYYY`)
}
get wordCount() {
return this.asTxt.match(/\b\w+\b/g)?.length || 0
}
get minutes() {
return (this.wordCount / 200).toFixed(1)
}
get hasKeyboardNav() {
return this.scrollProgram.has(scrollKeywords.keyboardNav)
}
// Get the line number that the snippet should stop at.
get endSnippetIndex() {
// First if its hard coded, use that
if (this.scrollProgram.has(scrollKeywords.endSnippet)) return this.scrollProgram.getParticle(scrollKeywords.endSnippet).getIndex()
// Next look for a dinkus
const snippetBreak = this.scrollProgram.find(particle => particle.isDinkus)
if (snippetBreak) return snippetBreak.getIndex()
return -1
}
timeIndex = 0
_nextAndPrevious(arr, index) {
const nextIndex = index + 1
const previousIndex = index - 1
return {
previous: arr[previousIndex] ?? arr[arr.length - 1],
next: arr[nextIndex] ?? arr[0]
}
}
// keyboard nav is always in the same folder. does not currently support cross folder
includeFileInKeyboardNav(file) {
return file.buildsHtml && file.hasKeyboardNav && file.tags.includes(this.primaryTag)
}
get linkToPrevious() {
if (!this.hasKeyboardNav)
// Dont provide link to next unless keyboard nav is on
return undefined
let file = this._nextAndPrevious(this.allScrollFiles, this.timeIndex).previous
while (!this.includeFileInKeyboardNav(file)) {
file = this._nextAndPrevious(this.allScrollFiles, file.timeIndex).previous
}
return file.permalink
}
get linkToNext() {
if (!this.hasKeyboardNav)
// Dont provide link to next unless keyboard nav is on
return undefined
let file = this._nextAndPrevious(this.allScrollFiles, this.timeIndex).next
while (!this.includeFileInKeyboardNav(file)) {
file = this._nextAndPrevious(this.allScrollFiles, file.timeIndex).next
}
return file.permalink
}
get canonicalUrl() {
return this.get(scrollKeywords.canonicalUrl) || this.baseUrl + this.permalink
}
ensureAbsoluteLink(link) {
if (link.includes("://")) return link
return this.baseUrl + link.replace(/^\//, "")
}
get openGraphImage() {
const openGraphImage = this.get(scrollKeywords.openGraphImage)
if (openGraphImage !== undefined) return this.ensureAbsoluteLink(openGraphImage)
const images = this.scrollProgram.filter(particle => particle.doesExtend("scrollImageParser"))
const hit = images.find(particle => particle.has(scrollKeywords.openGraph)) || images[0]
if (!hit) return ""
return this.ensureAbsoluteLink(hit.filename)
}
// Use the first paragraph for the description
// todo: add a particle method version of get that gets you the first particle. (actulaly make get return array?)
// would speed up a lot.
get description() {
const program = this.scrollProgram
const description = program.get(scrollKeywords.description)
if (description) return description
for (let particle of program.getTopDownArrayIterator()) {
if (particle.constructor.name !== "printTitleParser" && particle.doesExtend("paragraphParser")) return Utils.stripHtml(particle.compile()).replace(/\n/g, " ").replace(/\"/g, "'").substr(0, 300)
}
return ""
}
get title() {
const title = this.scrollProgram.get(scrollKeywords.title)
if (title) return title
else if (this.filename) return unCamelCase(this.filenameNoExtension)
return ""
}
get(parserAtom) {
return this.scrollProgram.get(parserAtom)
}
has(parserAtom) {
return this.scrollProgram.has(parserAtom)
}
get viewSourceUrl() {
const viewSourceUrl = this.get(scrollKeywords.viewSourceUrl)
if (viewSourceUrl) return viewSourceUrl
const viewSourceBaseUrl = this.get(scrollKeywords.viewSourceBaseUrl)
return (viewSourceBaseUrl ? viewSourceBaseUrl.replace(/\/$/, "") + "/" : "") + this.filename
}
get gitRepo() {
// given https://github.com/breck7/breckyunits.com/blob/main/four-tips-to-improve-communication.scroll
// return https://github.com/breck7/breckyunits.com
return this.viewSourceUrl.split("/").slice(0, 5).join("/")
}
get tags() {
return this.scrollProgram.get(scrollKeywords.tags) || ""
}
get primaryTag() {
return this.tags.split(" ")[0]
}
getFilesWithTagsForEmbedding(tags, limit) {
if (typeof tags === "string") tags = tags.split(" ")
if (!tags || !tags.length)
return this.allHtmlFiles
.filter(file => file !== this) // avoid infinite loops. todo: think this through better.
.map(file => {
return { file, relativePath: "" }
})
.slice(0, limit)
let arr = []
tags.forEach(name => {
if (!name.includes("/"))
return (arr = arr.concat(
getFilesWithTag(name, this.allScrollFiles)
.map(file => {
return { file, relativePath: "" }
})
.slice(0, limit)
))
const parts = name.split("/")
const group = parts.pop()
const relativePath = parts.join("/")
const folderPath = path.join(this.folderPath, path.normalize(relativePath))
const files = this.fileSystem.getScrollFilesInFolder(folderPath)
const filtered = getFilesWithTag(group, files).map(file => {
return { file, relativePath: relativePath + "/" }
})
arr = arr.concat(filtered.slice(0, limit))
})
return lodash.sortBy(arr, file => file.file.timestamp).reverse()
}
get viewSourceHtml() {
return this.compileStumpCode(`a View source
class abstractTextLinkParser
href ${this.viewSourceUrl}`)
}
_compiledHtml = ""
get asHtml() {
if (!this._compiledHtml) {
const { permalink, buildsHtml } = this
const content = (this.scrollProgram.compile() + this.scrollProgram.clearBodyStack()).trim()
// Don't add html tags to 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 = buildsHtml
const bodyTag = this.scrollProgram.has("metaTags") ? "" : "<body>\n"
this._compiledHtml = wrapWithHtmlTags ? `<!DOCTYPE html>\n<html lang="${this.lang}">\n${bodyTag}${content}\n</body>\n</html>` : content
}
return this._compiledHtml
}
get asTxt() {
return (
this.scrollProgram
.map(particle => {
const text = particle.compileTxt ? particle.compileTxt() : ""
if (text) return text + "\n"
if (!particle.getLine().length) return "\n"
return ""
})
.join("")
.replace(/<[^>]*>/g, "")
.replace(/\n\n\n+/g, "\n\n") // Maximum 2 newlines in a row
.trim() + "\n" // Always end in a newline, Posix style
)
}
get asJs() {
return this.scrollProgram.topDownArray
.filter(particle => particle.compileJs)
.map(particle => particle.compileJs())
.join("\n")
.trim()
}
get authors() {
return this.scrollProgram.get("authors")
}
get asRss() {
return this.scrollProgram.compile().trim()
}
get asCss() {
return this.scrollProgram.topDownArray
.filter(particle => particle.compileCss)
.map(particle => particle.compileCss())
.join("\n")
.trim()
}
get asCsv() {
return this.scrollProgram.topDownArray
.filter(particle => particle.compileCsv)
.map(particle => particle.compileCsv())
.join("\n")
.trim()
}
async build() {
await Promise.all(this.scrollProgram.filter(particle => particle.build).map(async particle => particle.build()))
}
// Without specifying the language hyphenation will not work.
get lang() {
return this.get("htmlLang") || "en"
}
// todo: rename publishedUrl? Or something to indicate that this is only for stuff on the web (not localhost)
// BaseUrl must be provided for RSS Feeds and OpenGraph tags to work
// maybe wwwBaseUrl?
get baseUrl() {
return (this.scrollProgram.get(scrollKeywords.baseUrl) ?? "").replace(/\/$/, "") + "/"
}
csvFields = CSV_FIELDS
toCsv() {
return CSV_FIELDS.map(field => escapeCommas(this[field]))
}
toRss() {
const { title, canonicalUrl } = this
return ` <item>
<title>${title}</title>
<link>${canonicalUrl}</link>
<pubDate>${dayjs(this.timestamp * 1000).format("ddd, DD MMM YYYY HH:mm:ss ZZ")}</pubDate>
</item>`
}
}
const isUserPipingInput = () => {
if (process.platform === "win32") return false
// Check if stdin is explicitly set to a TTY
if (process.stdin.isTTY === true) return false
return fs.fstatSync(0).isFIFO()
}
class ScrollCli {
CommandFnDecoratorSuffix = "Command"
executeUsersInstructionsFromShell(args = [], userIsPipingInput = isUserPipingInput()) {
const command = args[0] // Note: we don't take any parameters on purpose. Simpler UX.
const commandName = `${command}${this.CommandFnDecoratorSuffix}`
if (this[commandName]) return userIsPipingInput ? this._runCommandOnPipedStdIn(commandName) : this[commandName](process.cwd())
else if (command) this.log(`No command '${command}'. Running help command.`)
else this.log(`No command provided. Running help command.`)
return this.helpCommand()
}
_runCommandOnPipedStdIn(commandName) {
this.log(`Running ${commandName} on piped input`)
let pipedData = ""
process.stdin.on("readable", function () {
pipedData += this.read() // todo: what's the lambda way to do this?
})
process.stdin.on("end", () => {
const folders = pipedData
.trim()
.split("\n")
.map(line => line.trim())
.filter(line => fs.existsSync(line))
folders.forEach(line => this[commandName](line))
if (folders.length === 0)
// Hacky to make sure this at least does something in all environments.
// process.stdin.isTTY is not quite accurate for pipe detection
this[commandName](process.cwd())
})
}
silence() {
this.verbose = false
return this
}
verbose = true
logIndent = 0
log(message) {
const indent = " ".repeat(this.logIndent)
if (this.verbose) console.log(indent + message)
return message
}
get _allCommands() {
return Object.getOwnPropertyNames(Object.getPrototypeOf(this))
.filter(atom => atom.endsWith(this.CommandFnDecoratorSuffix))
.sort()
}
async initCommand(cwd) {
// todo: use stamp for this.
const initFolder = {
".gitignore": `*.html
*.txt
feed.xml`,
"header.scroll": `importOnly
metaTags
buildTxt
buildHtml
theme gazette
homeButton
leftRightButtons
viewSourceButton
printTitle`,
"feed.scroll": `buildRss feed.xml
printFeed All`,
"footer.scroll": `importOnly
center
viewSourceButton
scrollVersionLink`,
"helloWorld.scroll": `tags All
header.scroll
thinColumn
This is my first blog post using Scroll.
****
footer.scroll`,
"index.scroll": `title My Blog
header.scroll
printSnippets All
footer.scroll`
}
this.log(`Initializing scroll in "${cwd}"`)
Disk.writeObjectToDisk(cwd, initFolder)
require("child_process").execSync("git init", { cwd })
return this.log(`\n✅ Initialized new scroll in '${cwd}'. Build your new site with: scroll build`)
}
deleteCommand() {
return this.log(`\n💡 To delete a Scroll just delete the folder\n`)
}
// Normalize 3 possible inputs: 1) cwd of the process 2) provided absolute path 3) cwd of process + provided relative path
resolvePath(folder = "") {
return path.isAbsolute(folder) ? path.normalize(folder) : path.resolve(path.join(process.cwd(), folder))
}
getErrorsInFolder(folder) {
const fileSystem = new ScrollFileSystem()
const folderPath = Utils.ensureFolderEndsInSlash(folder)
fileSystem.getScrollFilesInFolder(folderPath) // Init all parsers
const parserErrors = fileSystem.parsers.map(parser => parser.getAllErrors().map(err => err.toObject())).flat()
const scrollErrors = fileSystem
.getScrollFilesInFolder(folderPath)
.map(file =>
file.scrollProgram.getAllErrors().map(err => {
return { filename: file.filename, ...err.toObject() }
})
)
.flat()
return { parserErrors, scrollErrors }
}
testCommand(cwd) {
const start = Date.now()
const folder = this.resolvePath(cwd)
const { parserErrors, scrollErrors } = this.getErrorsInFolder(folder)
const seconds = (Date.now() - start) / 1000
if (parserErrors.length) {
this.log(``)
this.log(`❌ ${parserErrors.length} parser errors in "${cwd}"`)
this.log(new Particle(parserErrors).toFormattedTable(200))
this.log(``)
}
if (scrollErrors.length) {
this.log(``)
this.log(`❌ ${scrollErrors.length} errors in "${cwd}"`)
this.log(new Particle(scrollErrors).toFormattedTable(100))
this.log(``)
}
if (!parserErrors.length && !scrollErrors.length) return this.log(`✅ 0 errors in "${cwd}". Tests took ${seconds} seconds.`)
return `${parserErrors.length + scrollErrors.length} Errors`
}
formatCommand(cwd) {
const fileSystem = new ScrollFileSystem()
const folder = this.resolvePath(cwd)
const files = fileSystem.getScrollFilesInFolder(folder)
// .concat(fileSystem.getParserFilesInFolder(folder)) // todo: should format parser files too.
files.forEach(file => (file.formatAndSave() ? this.log(`💾 formatted ${file.filename}`) : ""))
}
async buildCommand(cwd) {
this.buildFilesInFolder(new ScrollFileSystem(), this.resolvePath(cwd))
return this
}
_parserAtomsRequiringExternals(parser) {
// todo: could be cleaned up a bit
if (!parser.parserAtomsRequiringExternals) parser.parserAtomsRequiringExternals = getCruxAtoms(parser.cachedHandParsersProgramRoot.filter(particle => particle.copyFromExternal))
return parser.parserAtomsRequiringExternals
}
externalFilesCopied = {}
_copyExternalFiles(file, folder, fileSystem) {
// If this file uses a parser that has external requirements,
// copy those from external folder into the destination folder.
const parserAtomsRequiringExternals = this._parserAtomsRequiringExternals(file.parser)
const { externalFilesCopied } = this
if (!externalFilesCopied[folder]) externalFilesCopied[folder] = {}
parserAtomsRequiringExternals.forEach(atom => {
if (externalFilesCopied[folder][atom]) return
if (!file.has(atom)) return
file.scrollProgram.findParticles(atom).map(particle => {
const externalFiles = particle.copyFromExternal.split(" ")
externalFiles.forEach(name => {
const newPath = path.join(folder, name)
fileSystem.writeProduct(newPath, Disk.read(path.join(__dirname, "external", name)))
this.log(`💾 Copied external file needed by ${file.filename} to ${name}`)
})
})
if (atom !== "theme")
// todo: generalize when not to cache
externalFilesCopied[folder][atom] = true
})
}