-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: more consistent tags to make it easier to iterate over, #524
- Loading branch information
Showing
19 changed files
with
225 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# LiquidJS for Node.js | ||
|
||
## Get Started | ||
|
||
```bash | ||
cd demo/nodejs | ||
npm install | ||
npm start | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Output, Template, Tag } from 'liquidjs' | ||
import { isLayoutTag, isIfTag, isUnlessTag, isLiquidTag, isCaseTag, isCaptureTag, isTablerowTag, isForTag } from './type-guards' | ||
|
||
/** | ||
* iterate over all `{{ output }}` | ||
*/ | ||
export function * getOutputs (templates: Template[]): Generator<Output, void> { | ||
for (const template of templates) { | ||
if (template instanceof Tag) { | ||
if (isIfTag(template) || isUnlessTag(template) || isCaseTag(template)) { | ||
for (const branch of template.branches) { | ||
yield * getOutputs(branch.templates) | ||
} | ||
yield * getOutputs(template.elseTemplates) | ||
} else if (isForTag(template)) { | ||
yield * getOutputs(template.templates) | ||
yield * getOutputs(template.elseTemplates) | ||
} else if (isLiquidTag(template) || isCaptureTag(template) || isTablerowTag(template) || isLayoutTag(template)) { | ||
yield * getOutputs(template.templates) | ||
} | ||
} else if (template instanceof Output) { | ||
yield template | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Liquid } from 'liquidjs' | ||
import { getOutputs } from './get-outputs' | ||
|
||
const engine = new Liquid({ | ||
root: __dirname, | ||
extname: '.liquid' | ||
}) | ||
|
||
const templates = engine.parseFileSync('todolist') | ||
|
||
for (const output of getOutputs(templates)) { | ||
const token = output.token | ||
const [line, col] = token.getPosition() | ||
const text = token.getText() | ||
console.log(`[${line}:${col}] ${text}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "liquidjs-demo-template", | ||
"private": true, | ||
"description": "liquid template parsing", | ||
"main": "index.ts", | ||
"scripts": { | ||
"start": "ts-node index.ts" | ||
}, | ||
"dependencies": { | ||
"liquidjs": "latest", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^4.2.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.14.37" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% layout 'html.liquid' %} | ||
|
||
<h1>{{ title | capitalize }}</h1> | ||
<a> | ||
{% if authed %} Sign out {{ username }} | ||
{% else %} Sign in | ||
{% endif %} | ||
</a> | ||
<ul> | ||
{% for todo in todos %} | ||
<p>{{ todo }}</p> | ||
{% endfor %} | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"downlevelIteration": true, | ||
"types": [ | ||
"node" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { LayoutTag, ForTag, LiquidTag, CaptureTag, CaseTag, UnlessTag, TablerowTag, Tag, IfTag } from 'liquidjs' | ||
|
||
export function isIfTag (tag: Tag): tag is IfTag { | ||
return tag.name === 'if' | ||
} | ||
|
||
export function isUnlessTag (tag: Tag): tag is UnlessTag { | ||
return tag.name === 'unless' | ||
} | ||
|
||
export function isLiquidTag (tag: Tag): tag is LiquidTag { | ||
return tag.name === 'liquid' | ||
} | ||
|
||
export function isCaseTag (tag: Tag): tag is CaseTag { | ||
return tag.name === 'case' | ||
} | ||
|
||
export function isCaptureTag (tag: Tag): tag is CaptureTag { | ||
return tag.name === 'capture' | ||
} | ||
|
||
export function isTablerowTag (tag: Tag): tag is TablerowTag { | ||
return tag.name === 'tablerow' | ||
} | ||
|
||
export function isForTag (tag: Tag): tag is ForTag { | ||
return tag.name === 'for' | ||
} | ||
|
||
export function isLayoutTag (tag: Tag): tag is LayoutTag { | ||
return tag.name === 'layout' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
import assign from './assign' | ||
import For from './for' | ||
import capture from './capture' | ||
import Case from './case' | ||
import comment from './comment' | ||
import include from './include' | ||
import render from './render' | ||
import decrement from './decrement' | ||
import cycle from './cycle' | ||
import If from './if' | ||
import increment from './increment' | ||
import layout from './layout' | ||
import block from './block' | ||
import raw from './raw' | ||
import tablerow from './tablerow' | ||
import unless from './unless' | ||
import Break from './break' | ||
import Continue from './continue' | ||
import echo from './echo' | ||
import liquid from './liquid' | ||
import inlineComment from './inline-comment' | ||
import AssignTag from './assign' | ||
import ForTag from './for' | ||
import CaptureTag from './capture' | ||
import CaseTag from './case' | ||
import CommentTag from './comment' | ||
import IncludeTag from './include' | ||
import RenderTag from './render' | ||
import DecrementTag from './decrement' | ||
import CycleTag from './cycle' | ||
import IfTag from './if' | ||
import IncrementTag from './increment' | ||
import LayoutTag from './layout' | ||
import BlockTag from './block' | ||
import RawTag from './raw' | ||
import TablerowTag from './tablerow' | ||
import UnlessTag from './unless' | ||
import BreakTag from './break' | ||
import ContinueTag from './continue' | ||
import EchoTag from './echo' | ||
import LiquidTag from './liquid' | ||
import InlineCommentTag from './inline-comment' | ||
import type { TagClass } from '../template/tag' | ||
|
||
export const tags: Record<string, TagClass> = { | ||
assign, 'for': For, capture, 'case': Case, comment, include, render, decrement, increment, cycle, 'if': If, layout, block, raw, tablerow, unless, 'break': Break, 'continue': Continue, echo, liquid, '#': inlineComment | ||
assign: AssignTag, | ||
'for': ForTag, | ||
capture: CaptureTag, | ||
'case': CaseTag, | ||
comment: CommentTag, | ||
include: IncludeTag, | ||
render: RenderTag, | ||
decrement: DecrementTag, | ||
increment: IncrementTag, | ||
cycle: CycleTag, | ||
'if': IfTag, | ||
layout: LayoutTag, | ||
block: BlockTag, | ||
raw: RawTag, | ||
tablerow: TablerowTag, | ||
unless: UnlessTag, | ||
'break': BreakTag, | ||
'continue': ContinueTag, | ||
echo: EchoTag, | ||
liquid: LiquidTag, | ||
'#': InlineCommentTag | ||
} | ||
|
||
export { AssignTag, ForTag, CaptureTag, CaseTag, CommentTag, IncludeTag, RenderTag, DecrementTag, IncrementTag, CycleTag, IfTag, LayoutTag, BlockTag, RawTag, TablerowTag, UnlessTag, BreakTag, ContinueTag, EchoTag, LiquidTag, InlineCommentTag } |
Oops, something went wrong.