Skip to content

Commit

Permalink
Merge pull request #13 from cedar-policy/feat/hakanson/cedarschema
Browse files Browse the repository at this point in the history
feat: Cedar human-readable schema
  • Loading branch information
hakanson authored Apr 22, 2024
2 parents 35668e6 + 79b6570 commit d64df7e
Show file tree
Hide file tree
Showing 24 changed files with 876 additions and 207 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build_and_test:
runs-on: ubuntu-latest

steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Install Node 20
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Test
run: |
npm ci
npm run test
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## v0.4.0 2024-04-20

- Add Cedar human-readable schema support
- Update copyright to Cedar Contributors

## v0.3.0 2024-01-20

- Update pattern matching
Expand Down
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
# highlight.js support for Cedar policy language
# highlight.js support for Cedar policy language and Cedar human-readable schema

[highlight.js](https://highlightjs.org/) (syntax highlighting for the Web) support for Cedar based on [Grammar specification for Cedar policy syntax](https://docs.cedarpolicy.com/syntax-grammar.html) based on structure from [language contributor checklist](https://highlightjs.readthedocs.io/en/latest/language-contribution.html) and mapped to the [Stylable Scopes](https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html).
[highlight.js](https://highlightjs.org/) (syntax highlighting for the Web) support for Cedar based on [Grammar specification for Cedar policy syntax](https://docs.cedarpolicy.com/syntax-grammar.html) and for Cedar human-readable schema based on [Grammar specification for human-readable schemas](https://docs.cedarpolicy.com/schema/human-readable-schema-grammar.html). Project structure derived from [language contributor checklist](https://highlightjs.readthedocs.io/en/latest/language-contribution.html) and mapped to the [Stylable Scopes](https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html).

See it in action on <https://docs.cedarpolicy.com/>

## Install
## Use

Web browser `<script src=>` usage example from `test/static/index.html` (adjust the script `src` paths as required):

```html
<script src="highlight.min.js"></script>
<script src="hljs-cedar.min.js"></script>
<script type="text/javascript">
window.onload = () => {
hljs.registerLanguage('cedar', hljsCedar);
hljs.registerLanguage('cedarschema', hljsCedarschema);
hljs.highlightAll();
};
</script>
```

ECMAScript module `import` usage example from `test/vite/index.html` (adjust the import `from` paths as required):

```javascript
import hljs from 'highlight.js';
import { hljsCedar, hljsCedarschema } from 'hljs-cedar.mjs';
hljs.registerLanguage('cedar', hljsCedar);
hljs.registerLanguage('cedarschema', hljsCedarschema);
hljs.highlightAll();
```

## Development

### Install

Install the project dependencies using `npm install`.

`src/cedar.js` is the main source file in ECMAScript module format.

## Build
### Build

The `npm run build` script uses [esbuild](https://esbuild.github.io/api/)to create multiple files in the `dist` folder:

- `hljs-cedar.js` - Web browser `<script src=>` compatible version. Sets `window.hljsCedar` to highlight function.
- `hljs-cedar.js` - Web browser `<script src=>` compatible version. Sets `window.hljsCedar` and `window.hljsCedarschema` to highlight functions.
- `hljs-cedar.min.js` - Minified web browser `<script src=>` compatible version.
- `hljs-cedar.mjs` - ECMAScript module version with default export. Used by `test/cedar-esm.test.js` test cases and `test\vite\main.js` from Vite web app.
- `import hljsCedar from '../dist/hljs-cedar.mjs';`
- `hljs-cedar.cjs` - CommonJS module version with default export. Used by `test/cedar-commonjs.test.js` test cases.
- `const hljsCedar = require('../dist/hljs-cedar.cjs').default;`

## Test
### Test

`npm run test` uses [vitest](https://vitest.dev) to syntax highlight `test/data/*.cedar` files and compares against a generated `.html`
[File Snapshots](https://vitest.dev/guide/snapshot.html#file-snapshots) for each Cedar file.
`npm run test` uses [vitest](https://vitest.dev) to syntax highlight `test/data/*.cedar` and `test/data/*.cedarschema` files and compares against a generated `.html`
[File Snapshots](https://vitest.dev/guide/snapshot.html#file-snapshots) for each Cedar and Cedar human-readable schema file.

View either the static `test/static/index.html` on your browser or `npm run dev` and `test/vite/index.html` renders in your browser: <http://localhost:5173/>

When new `test/data/*.cedar` files are created, `npm run testdata` will update the contents of each `index.html` file.
When new `test/data/*.cedar` or `test/data/*.cedarschema` files are created, `npm run testdata` will update the contents of each `index.html` file.

## Security

Expand Down
8 changes: 8 additions & 0 deletions dist/github.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions dist/hljs-cedar.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ var __copyProps = (to, from, except, desc) => {
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cedar_exports = {};
__export(cedar_exports, {
default: () => cedar_default
default: () => cedar_default,
hljsCedar: () => hljsCedar,
hljsCedarschema: () => hljsCedarschema
});
module.exports = __toCommonJS(cedar_exports);
//! Copyright Cedar Contributors
//! SPDX-License-Identifier: Apache-2.0
function cedar_default(hljs) {
function hljsCedar(hljs) {
const GLOBALS = {
match: /\b(?:ip|decimal)(?=\()/,
scope: "built_in"
Expand Down Expand Up @@ -122,3 +124,40 @@ function cedar_default(hljs) {
]
};
}
function hljsCedarschema(hljs) {
const KEYWORDS = {
match: /\b(?:namespace|type|entity|action)(?=\s+)/,
scope: "keyword"
};
const MOREKEYWORDS = {
begin: "\\b(?:in)\\b|\\b(?:appliesTo)(?=\\s*{)",
scope: "keyword"
};
const OPERATORS = {
begin: "=",
scope: "operator"
};
const PUNCTUATION = {
begin: "{|}|\\[|]|;",
scope: "punctuation"
};
const ATTRS = {
begin: /\b(?:[_a-zA-Z][_a-zA-Z0-9]*)(?=[?]?:(?!:))/,
scope: "property"
};
return {
name: "Cedar schema",
aliases: ["cedarschema"],
case_insensitive: false,
contains: [
hljs.QUOTE_STRING_MODE,
hljs.C_LINE_COMMENT_MODE,
KEYWORDS,
MOREKEYWORDS,
OPERATORS,
PUNCTUATION,
ATTRS
]
};
}
var cedar_default = hljsCedar;
41 changes: 39 additions & 2 deletions dist/hljs-cedar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(() => {
// src/cedar.js
function cedar_default(hljs) {
function hljsCedar(hljs) {
const GLOBALS = {
match: /\b(?:ip|decimal)(?=\()/,
scope: "built_in"
Expand Down Expand Up @@ -100,9 +100,46 @@
]
};
}
function hljsCedarschema(hljs) {
const KEYWORDS = {
match: /\b(?:namespace|type|entity|action)(?=\s+)/,
scope: "keyword"
};
const MOREKEYWORDS = {
begin: "\\b(?:in)\\b|\\b(?:appliesTo)(?=\\s*{)",
scope: "keyword"
};
const OPERATORS = {
begin: "=",
scope: "operator"
};
const PUNCTUATION = {
begin: "{|}|\\[|]|;",
scope: "punctuation"
};
const ATTRS = {
begin: /\b(?:[_a-zA-Z][_a-zA-Z0-9]*)(?=[?]?:(?!:))/,
scope: "property"
};
return {
name: "Cedar schema",
aliases: ["cedarschema"],
case_insensitive: false,
contains: [
hljs.QUOTE_STRING_MODE,
hljs.C_LINE_COMMENT_MODE,
KEYWORDS,
MOREKEYWORDS,
OPERATORS,
PUNCTUATION,
ATTRS
]
};
}

// src/webbundle.js
window.hljsCedar = cedar_default;
window.hljsCedar = hljsCedar;
window.hljsCedarschema = hljsCedarschema;
})();
//! Copyright Cedar Contributors
//! SPDX-License-Identifier: Apache-2.0
2 changes: 1 addition & 1 deletion dist/hljs-cedar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions dist/hljs-cedar.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Copyright Cedar Contributors
//! SPDX-License-Identifier: Apache-2.0
function cedar_default(hljs) {
function hljsCedar(hljs) {
const GLOBALS = {
match: /\b(?:ip|decimal)(?=\()/,
scope: "built_in"
Expand Down Expand Up @@ -100,6 +100,45 @@ function cedar_default(hljs) {
]
};
}
function hljsCedarschema(hljs) {
const KEYWORDS = {
match: /\b(?:namespace|type|entity|action)(?=\s+)/,
scope: "keyword"
};
const MOREKEYWORDS = {
begin: "\\b(?:in)\\b|\\b(?:appliesTo)(?=\\s*{)",
scope: "keyword"
};
const OPERATORS = {
begin: "=",
scope: "operator"
};
const PUNCTUATION = {
begin: "{|}|\\[|]|;",
scope: "punctuation"
};
const ATTRS = {
begin: /\b(?:[_a-zA-Z][_a-zA-Z0-9]*)(?=[?]?:(?!:))/,
scope: "property"
};
return {
name: "Cedar schema",
aliases: ["cedarschema"],
case_insensitive: false,
contains: [
hljs.QUOTE_STRING_MODE,
hljs.C_LINE_COMMENT_MODE,
KEYWORDS,
MOREKEYWORDS,
OPERATORS,
PUNCTUATION,
ATTRS
]
};
}
var cedar_default = hljsCedar;
export {
cedar_default as default
cedar_default as default,
hljsCedar,
hljsCedarschema
};
Loading

0 comments on commit d64df7e

Please sign in to comment.