First Commit

This commit is contained in:
2025-11-02 22:52:08 +01:00
commit 73fbbf1be2
5821 changed files with 977526 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
{
"theme": "hide-semicolons"
}

View File

@@ -0,0 +1,7 @@
# Cardinal Examples
You can run the examples individually or as a demo:
- install cardinal: `npm install cardinal`
- explore cardinal: `npm explore cardinal`
- run the demo: `npm run demo`

View File

@@ -0,0 +1,78 @@
diff --git a/test/settings.js b/test/settings.js
index 7b28d66..642688f 100644
--- a/test/settings.js
+++ b/test/settings.js
@@ -1,14 +1,20 @@
'use strict';
/*jshint asi: true*/
-var test = require('tap').test
- , path = require('path')
- , fs = require('fs')
- , settings = require('../settings')
- , existsSync = fs.existsSync || path.existsSync
+var test = require('tap').test
+ , path = require('path')
+ , fs = require('fs')
, hideSemicolonsTheme = require('../themes/hide-semicolons')
, home = path.join(__dirname, 'fixtures', 'home')
, rcpath = path.join(home, '.cardinalrc')
+ , existsSync = fs.existsSync || path.existsSync
+ , settingsResolve = require.resolve('../settings')
+ , settings
+
+function setup () {
+ delete require.cache[settingsResolve]
+ settings = require(settingsResolve)
+}
function writerc(config) {
fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8')
@@ -25,22 +31,47 @@ function resolveTheme (config) {
return result;
}
+function getSettings (config) {
+ writerc(config)
+ var result = settings.getSettings(home)
+ removerc()
+ return result;
+}
+
if (!existsSync(home)) fs.mkdirSync(home);
test('no .cardinalrc in home', function (t) {
+ setup()
var theme = settings.resolveTheme(home)
t.equals(theme, undefined, 'resolves no theme')
t.end()
})
test('.cardinalrc with theme "hide-semicolons" in home', function (t) {
+ setup()
var theme = resolveTheme({ theme: "hide-semicolons" })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
test('.cardinalrc with full path to "hide-semicolons.js" in home', function (t) {
+ setup()
var theme = resolveTheme({ theme: path.join(__dirname, '..', 'themes', 'hide-semicolons.js') })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
+
+test('.cardinalrc with linenos: true', function (t) {
+ setup()
+ var opts = { linenos: true }
+ t.deepEquals(getSettings(opts), opts)
+ t.end()
+})
+
+test('.cardinalrc with linenos: true and theme', function (t) {
+ setup()
+ var opts = { linenos: true, theme: 'some theme' }
+ t.deepEquals(getSettings(opts), opts)
+ t.end()
+})
+

View File

@@ -0,0 +1,90 @@
'use strict'
var fs = require('fs')
var path = require('path')
var highlighter = require('..')
var colors = require('ansicolors')
var diffFile = path.join(__dirname, 'git-diff.txt')
var diff = fs.readFileSync(diffFile, 'utf-8')
// @@ is not a valid js token, so when we see it, we can be sure that we are dealing with a git or svn diff
var diffRegex = /^@@[^@]+@@$/m
var diffIndRegex = /^(@@[^@]+@@)(.*)$/
var addRemRegex = /^[+-]/
var lines = diff.split('\n')
function isDiff(lines) {
return !!lines
.filter(function(line) {
return diffRegex.test(line)
})
.length
}
diff = isDiff(lines)
function tryHighlight(code) {
// TODO: need to remove symbols added to get valid code
// this should be done by getting the splits instead of the actual code from the highlighter
// now we can remove first / last one after highlighting completed
function tryAppending(appended, tryNext) {
try {
return highlighter.highlight(code + appended)
} catch (e) {
return tryNext(code)
}
}
function tryRemoveLeadingComma(tryNext) {
var success
try {
success = highlighter.highlight(code.replace(/^( +),(.+)$/, '$1 $2'))
return success
} catch (e) {
return tryNext(code)
}
}
function tryPlain() {
try {
return highlighter.highlight(code)
} catch (e) {
return tryCloseMustache()
}
}
function tryCloseMustache() { return tryAppending('}', tryCloseParen) }
function tryCloseParen() { return tryAppending('\\)', tryCloseMustacheParen) }
function tryCloseMustacheParen() { return tryAppending('})', tryRemovingCommas) }
function tryRemovingCommas() { return tryRemoveLeadingComma(giveUp) }
function giveUp() { return code }
return tryPlain()
}
function highlightDiffInd(line, matches) {
var highlighted = colors.brightBlue(matches[1])
var code = matches[2]
return code ? highlighted + tryHighlight(code) : highlighted
}
function colorsAddRemove(c) {
return addRemRegex.test(c) ? colors.yellow(c) : c
}
function highlightDiff(line) {
var diffIndMatches = diffIndRegex.exec(line)
return diffIndMatches
? highlightDiffInd(line, diffIndMatches)
: colorsAddRemove(line[0]) + tryHighlight(line.slice(1))
}
var highlightFn = diff ? highlightDiff : tryHighlight
var highlightedLines = lines.map(highlightFn)
console.log(highlightedLines.join('\n'))

View File

@@ -0,0 +1,11 @@
// This file will highlight the passed code using the custom theme when run via: "node highlight-json"
'use strict'
var cardinal = require('..')
var json = JSON.stringify({
foo: 'bar',
baz: 'quux'
})
console.log(cardinal.highlight(json))

View File

@@ -0,0 +1,22 @@
/*
* This file will highlight itself using a custom theme when run via: "node highlight-self-hide-semicolons"
* The custom theme highlights semicolons as 'black', thus hiding them.
*/
'use strict'
var cardinal = require('..')
var hideSemicolonsTheme = require('../themes/hide-semicolons')
function highlight() {
// Using the synchronous highlightFileSync()
// For asynchronous highlighting use: highlightFile() - see highlight-self.js
try {
var highlighted = cardinal.highlightFileSync(__filename, {theme: hideSemicolonsTheme})
console.log(highlighted)
} catch (err) {
console.error(err)
}
}
highlight()

View File

@@ -0,0 +1,16 @@
// This file will highlight itself using the default theme when run via: "node highlight-self"
'use strict'
var cardinal = require('..')
function highlight() {
// Using the asynchronous highlightFile()
// For synchronous highlighting use: highlightFileSync() - see highlight-self-hide-semicolons.js
cardinal.highlightFile(__filename, { linenos: true }, function(err, res) {
if (err) return console.error(err)
console.log(res)
})
}
highlight()

View File

@@ -0,0 +1,15 @@
// This file will highlight the passed code using the custom theme when run via: "node highlight-string"
'use strict'
var cardinal = require('..')
var code = '' +
function add(a, b) {
var sum = a + b
return sum
} +
''
console.log(cardinal.highlight(code))