Skip to content

이 페이지는 번역본입니다. 영어 페이지와 내용이 다르면 영어 페이지가 기준입니다. English

구문 트리 ​

parsePug는 구체(concrete) 구문 트리를 반환합니다. 템플릿이 무엇을 의미하는지뿐 아니라 어떻게 쓰였는지도 유지합니다. 모든 축약 표기, 괄호, 쉼표, 따옴표, 줄에 range가 있으므로, 도구는 그중 어느 것이든 가리킬 수 있고 나머지를 건드리지 않고 하나만 고쳐 쓸 수도 있습니다.

PugDocument ​

ts
interface PugDocument {
  type: 'Document'
  text: string             // the text passed to parsePug
  range: Range             // [start, end) that was parsed
  children: PugNode[]      // the top-level nodes
  errors: PugParseError[]  // see Errors
  lines: PugLine[]         // one entry per source line, see Lines
  eol: '\n' | '\r\n'       // the first line ending found; '\n' if there is none
  indentUnit: string       // the indentation of the first nested line, relative to its parent; '  ' if none
}

eol과 indentUnit은 파일이 이미 어떤 줄바꿈과 들여쓰기를 쓰는지 포매터에게 알려 줍니다.

노드 ​

ts
type PugNode = Tag | MixinCall | Text | Comment | Code | Control | Filter

노드의 range는 자기 자신의 줄만 포함하고, 그 아래에 중첩된 줄은 포함하지 않습니다. 중첩된 줄은 children에 있습니다. 중첩된 줄을 가질 수 있는 노드는 Tag, MixinCall, Code, Control입니다.

Tag ​

a.btn#go(href="/x" :title="t") Go는 다음과 같이 파싱됩니다.

js
{
  type: 'Tag',
  range: [0, 33],                                   // the whole head line
  name: { value: 'a', range: [0, 1] },              // null for an implicit div: .card
  shorthands: [
    { kind: 'class', value: 'btn', range: [1, 5] }, // range includes the '.'
    { kind: 'id', value: 'go', range: [5, 8] },     // and the '#'
  ],
  attributeBlocks: [{ range: [8, 30], attributes: [/* see Attributes */], commas: [] }],
  andAttributes: [],                                // &attributes(obj): { range, expression }
  selfClose: null,                                  // the '/' of img/
  dot: null,                                        // the '.' of p. (text block)
  expansion: null,                                  // the ':' of li: a
  children: [{ type: 'Text', form: 'inline', range: [31, 33], content: [31, 33], parts: [/* … */] }],
}

인라인 텍스트(p Hi), buffered code(p= msg), 블록 확장의 자식(li: a), 중첩된 줄은 모두 children에 들어갑니다.

MixinCall ​

+card(title, 2).wide(x="1"): 태그와 비슷하며, name(+를 뺀 card)과 args(괄호를 포함한 인자의 range, (title, 2), 괄호가 없으면 null)가 있습니다. 태그와 마찬가지로 shorthands, attributeBlocks, andAttributes, children을 가집니다.

Text ​

ts
interface Text {
  type: 'Text'
  form: 'inline' | 'pipe' | 'block' | 'html'
  range: Range
  content: Range       // the text itself, after `| ` or the tag
  parts: TextPart[]
}
form소스
inlinep Hello (같은 줄에서 태그 뒤에 오는 텍스트)
pipe| Hello (줄마다 노드 하나)
blockp., script. 등의 아래에 있는 줄들
html리터럴 HTML 한 줄, <b>Hello</b>

parts는 텍스트를 일반 텍스트와 interpolation으로 나눕니다. p Hello #{name}, #[b bold] and {{ x }}의 경우는 다음과 같습니다.

Part소스
{ type: 'Raw' }Hello
{ type: 'Interpolation', escaped, expression }#{name} (!{…}는 escaped: false)
{ type: 'Raw' },
{ type: 'TagInterpolation', tag }#[b bold], tag는 완전한 Tag 노드
{ type: 'Raw' } and {{ x }}

{{ }}는 나누지 않습니다. Vue의 것이므로 일반 텍스트에 남습니다.

Comment, Code, Control, Filter ​

노드소스필드
Comment// shown, //- hidden, 그리고 그 아래에 중첩된 줄buffered(//는 true, //-는 false), content
Code- const x = 1, = value, != html, 태그의 자식인 p= valuebuffered, escaped, expression, children
Controlif, else if, else, unless, each, for, while, case, when, default, mixin, block, append, prepend, include, extends, doctype, yieldkeyword, head(줄의 나머지), children
Filter:markdown-it과 그 텍스트 줄name, attributeBlocks, children(Text 노드)

속성 ​

ts
interface AttributeBlock {
  range: Range        // includes '(' and ')'
  attributes: Attribute[]
  commas: Range[]     // the commas between attributes, if any
}

interface Attribute {
  range: Range
  key: { raw: string; range: Range }                  // href, :title, @click, v-model.trim
  operator: { value: '=' | '!='; range: Range } | null // null for a boolean attribute: disabled
  value: AttributeValue | null
}

interface AttributeValue {
  range: Range
  raw: string
  kind: 'string' | 'template' | 'expression'
  quote: '"' | "'" | '`' | null
  inner: Range | null       // between the quotes; null for an expression
  hasSubstitution: boolean  // a template literal with ${…}
}

태그는 블록을 둘 이상 가질 수 있으므로(a(x)(y)는 유효한 Pug) attributeBlocks는 목록입니다. 보통은 비어 있거나 항목이 하나입니다.

kind해당하는 경우예시
string값 전체가 '…' 또는 "…" 리터럴 하나href="/x"
template값 전체가 `…` 리터럴 하나title=`Hi ${name}`
expression그 밖의 모든 경우: Pug가 JavaScript로 컴파일합니다href=url, title="a" + b

Vue 입장에서 string 값은 directive나 속성이 받는 값입니다(:title="t"는 t를 바인딩합니다). expression 값은 Vue가 보기 전에 Pug가 컴파일 타임에 평가합니다.

줄 ​

doc.lines에는 파싱한 범위의 소스 줄마다 항목이 하나씩 있습니다. "이 줄에 무엇이 있고 얼마나 깊이 들여써야 하는가"에 답하는 정보로, 포매터에는 필요하지만 트리만으로는 알 수 없습니다.

ts
interface PugLine {
  range: Range          // the line, without its line ending
  indent: Range         // its leading whitespace
  kind: 'node' | 'attribute' | 'paren-close' | 'raw' | 'continuation' | 'blank'
  depth: number
  anchor: number | null // index into lines; see below
}

다음 템플릿을 예로 들면:

pug
ul#list
  li(
    v-for="x in xs"
    :key="x"
  ) {{ x }}
  p.
    some text
줄kinddepth
ul#listnode0
li(node1
v-for="x in xs"attribute2
:key="x"attribute2
) {{ x }}paren-close1
p.node1
some textraw2
(마지막 줄바꿈 뒤)blank0
kind줄depth
node노드를 시작함노드의 중첩 깊이
attribute여러 줄 속성 블록 안태그의 depth + 1
paren-close블록을 닫는 )로 시작함태그의 depth
raw텍스트 블록, 주석, code 블록, filter 안소유자의 depth + 1
continuation위의 줄에서 이어짐(여러 줄 값), 또는 에러 뒤에 건너뜀그 줄의 depth + 1
blank비어 있거나 공백뿐임0

anchor는 depth를 따르지 않고 다른 줄을 기준으로 한 열을 유지해야 하는 줄에 설정됩니다. 텍스트 블록의 첫 줄 이후 줄들(첫 줄에 anchor됨)과 continuation 줄(이어지는 줄에 anchor됨)이 그렇습니다. 다음 예에서

pug
p.
  one
    two
p(title=`a
  b`) x

two 줄은 one 줄에 anchor되고, title 값의 두 번째 줄은 바로 위 줄에 anchor됩니다. 포매터가 anchor가 되는 줄을 옮기면 이 줄들도 같은 만큼 옮깁니다.

MIT 라이선스로 배포됩니다.