Skip to content

Commit 596f1de

Browse files
committed
Initial Commit.
0 parents  commit 596f1de

File tree

5 files changed

+3823
-0
lines changed

5 files changed

+3823
-0
lines changed

.gitignore

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/node
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=node
3+
4+
### Node ###
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
.pnpm-debug.log*
13+
14+
# Diagnostic reports (https://nodejs.org/api/report.html)
15+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Directory for instrumented libs generated by jscoverage/JSCover
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
coverage
28+
*.lcov
29+
30+
# nyc test coverage
31+
.nyc_output
32+
33+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
34+
.grunt
35+
36+
# Bower dependency directory (https://bower.io/)
37+
bower_components
38+
39+
# node-waf configuration
40+
.lock-wscript
41+
42+
# Compiled binary addons (https://nodejs.org/api/addons.html)
43+
build/Release
44+
45+
# Dependency directories
46+
node_modules/
47+
jspm_packages/
48+
49+
# Snowpack dependency directory (https://snowpack.dev/)
50+
web_modules/
51+
52+
# TypeScript cache
53+
*.tsbuildinfo
54+
55+
# Optional npm cache directory
56+
.npm
57+
58+
# Optional eslint cache
59+
.eslintcache
60+
61+
# Optional stylelint cache
62+
.stylelintcache
63+
64+
# Microbundle cache
65+
.rpt2_cache/
66+
.rts2_cache_cjs/
67+
.rts2_cache_es/
68+
.rts2_cache_umd/
69+
70+
# Optional REPL history
71+
.node_repl_history
72+
73+
# Output of 'npm pack'
74+
*.tgz
75+
76+
# Yarn Integrity file
77+
.yarn-integrity
78+
79+
# dotenv environment variable files
80+
.env
81+
.env.development.local
82+
.env.test.local
83+
.env.production.local
84+
.env.local
85+
86+
# parcel-bundler cache (https://parceljs.org/)
87+
.cache
88+
.parcel-cache
89+
90+
# Next.js build output
91+
.next
92+
out
93+
94+
# Nuxt.js build / generate output
95+
.nuxt
96+
dist
97+
98+
# Gatsby files
99+
.cache/
100+
# Comment in the public line in if your project uses Gatsby and not Next.js
101+
# https://nextjs.org/blog/next-9-1#public-directory-support
102+
# public
103+
104+
# vuepress build output
105+
.vuepress/dist
106+
107+
# vuepress v2.x temp and cache directory
108+
.temp
109+
110+
# Docusaurus cache and generated files
111+
.docusaurus
112+
113+
# Serverless directories
114+
.serverless/
115+
116+
# FuseBox cache
117+
.fusebox/
118+
119+
# DynamoDB Local files
120+
.dynamodb/
121+
122+
# TernJS port file
123+
.tern-port
124+
125+
# Stores VSCode versions used for testing VSCode extensions
126+
.vscode-test
127+
128+
# yarn v2
129+
.yarn/cache
130+
.yarn/unplugged
131+
.yarn/build-state.yml
132+
.yarn/install-state.gz
133+
.pnp.*
134+
135+
### Node Patch ###
136+
# Serverless Webpack directories
137+
.webpack/
138+
139+
# Optional stylelint cache
140+
141+
# SvelteKit build / generate output
142+
.svelte-kit
143+
144+
# End of https://www.toptal.com/developers/gitignore/api/node
145+
146+
# Do not commit the compiled parser.
147+
bbcode-parser.js

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# bbcode-parser-sax - a bbcode parser with no dependencies and a simple API.
2+
3+
bbcode-parser-sax is a bbcode parser, writen with PEGjs. The PEGjs grammar is
4+
compiled into a parser library and does not have any dependencies.
5+
6+
The parser outputs a list of events in json, each with a specified type,
7+
similar to a SAX parser.
8+
9+
For example, given the string: `The quick brown fox [i]jumped[/i]
10+
[b]over[/b] the [u]stream[/u]`, the tool would emit:
11+
12+
```js
13+
[
14+
{ type: 'text', text: 'The quick brown ' },
15+
{ type: 'startTag', tag: { name: 'i', value: null } },
16+
{ type: 'text', text: 'fox' },
17+
{ type: 'endTag', tag: { name: 'i', value: null } },
18+
{ type: 'text', text: ' ' },
19+
{ type: 'startTag', tag: { name: 'b', value: null } },
20+
{ type: 'text', text: 'jumped' },
21+
{ type: 'endTag', tag: { name: 'b', value: null } }
22+
]
23+
```
24+
25+
Tags that have values, like `[url=https://site.com]` will be parsed
26+
as `tag: { name: 'url', value: 'https://site.com' }`.
27+
28+
This style allows the parser to be simple, and makes it easy to adapt
29+
the output to whatever you'd like!
30+
31+
## Building
32+
Run `yarn build` from the project directory to both generate the parser
33+
from the grammar, and to build cjs and esm outputs.
34+
35+
## Usage
36+
Usage will depend if you're using this with CommonJS or ESM
37+
38+
For CommonJS:
39+
```js
40+
const {parse} = require("bbcode-parser-sax")
41+
return parse(BBCODE_STRING)
42+
```
43+
44+
For ESM:
45+
```js
46+
import parser from 'bbcode-parser-sax'
47+
return parser.parse(BBCODE_STRING)
48+
```

grammar.pegjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Simple BBCode Parser in PEG
2+
// ==========================
3+
//
4+
// Produces SAX-style events based on what is parsed.
5+
// Can be reviewed and tested at https://pegjs.org/online
6+
7+
Expression
8+
= (EndTag / StartTag / String)*
9+
10+
TagChars
11+
= !("]" / " ") name:(. TagChars*) { return name.join("") }
12+
13+
TagCharsWithoutEquals
14+
= !("]" / " " / "=") name:(. TagCharsWithoutEquals*) { return name.join("") }
15+
16+
Tag
17+
= name:(TagChars) { return {"name": name, value: null } }
18+
19+
TagWithValue
20+
= name:(TagCharsWithoutEquals) "=" value:(TagChars) { return { name: name, value: value } }
21+
22+
StartTag
23+
= "[" tagName:(TagWithValue / Tag) "]" {return {type: "startTag", tag: tagName} }
24+
25+
EndTag
26+
= "[/" tagName:(TagWithValue / Tag) "]" {return {type: "endTag", tag: tagName} }
27+
28+
NotTag
29+
= !(EndTag / StartTag) s:(. NotTag*) { return s.join("") }
30+
31+
String
32+
= s:NotTag { return {type: "text", text: s} }

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "bbcode-parser-sax",
3+
"description": "A simple, dependency-free parser for BBCode, based on SAX.",
4+
"version": "1.0.0",
5+
"source": "bbcode-parser.js",
6+
"main": "dist/bbcode-parser.js",
7+
"module": "dist/bbcode-parser.esm.js",
8+
"license": "MIT",
9+
"scripts": {
10+
"build-parser": "pegjs -o bbcode-parser.js grammar.pegjs",
11+
"build": "yarn build-parser && parcel build"
12+
},
13+
"devDependencies": {
14+
"parcel": "^2.2.1",
15+
"pegjs": "^0.10.0"
16+
}
17+
}

0 commit comments

Comments
 (0)