Skip to content

यह पेज एक अनुवाद है। यदि यह अंग्रेज़ी पेज से अलग है, तो अंग्रेज़ी पेज ही सही है। English

Syntax tree ​

parsePug एक concrete syntax tree लौटाता है: template का मतलब ही नहीं, वह यह भी रखता है कि वह कैसे लिखा गया है। हर shorthand, parenthesis, comma, quote और line की एक range होती है, इसलिए कोई tool उनमें से किसी की ओर इशारा कर सकता है, या बाकी को छुए बिना एक को दोबारा लिख सकता है।

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 formatter को बताते हैं कि फ़ाइल पहले से कौन-सी line ending और indentation उपयोग करती है।

Nodes ​

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

Node की range उसकी अपनी line(s) को कवर करती है, उसके नीचे nested lines को नहीं; वे children में हैं। nested lines लेने वाले nodes Tag, MixinCall, Code और Control हैं।

Tag ​

a.btn#go(href="/x" :title="t") Go इस रूप में parse होता है:

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: [/* … */] }],
}

Inline text (p Hi), buffered code (p= msg), block expansion का child (li: a) और nested lines सब children में जाते हैं।

MixinCall ​

+card(title, 2).wide(x="1"): tag जैसा, name (card, + के बिना) और args के साथ, यानी arguments की range parentheses समेत, (title, 2) (उनके बिना null)। इसमें tag की तरह 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 (उसी line पर tag के बाद text)
pipe| Hello (हर line के लिए एक node)
blockp., script., … के नीचे की lines
htmlliteral HTML की एक line, <b>Hello</b>

parts text को raw text और interpolations में बाँटता है। 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 node है
{ type: 'Raw' } and {{ x }}

{{ }} को बाँटा नहीं जाता: वह Vue का है, और raw text में ही रहता है।

Comment, Code, Control, Filter ​

Nodeस्रोतFields
Comment// shown, //- hidden, और उनके नीचे nested linesbuffered (// है, //- नहीं), content
Code- const x = 1, = value, != html, और tag के child के रूप में 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 (line का बाकी हिस्सा), children
Filter:markdown-it, और उसकी text linesname, attributeBlocks, children (Text nodes)

Attributes ​

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 ${…}
}

Tag में एक से अधिक block हो सकते हैं (a(x)(y) वैध Pug है), इसलिए attributeBlocks एक सूची है; वह आमतौर पर खाली होती है या उसमें एक entry होती है।

kindकबउदाहरण
stringपूरा value एक '…' या "…" literal हैhref="/x"
templateपूरा value एक `…` literal हैtitle=`Hi ${name}`
expressionऔर कुछ भी: Pug उसे JavaScript के रूप में compile करता हैhref=url, title="a" + b

Vue के लिए, string value वही है जो directive या attribute को मिलता है (:title="t" t को bind करता है); expression value को Pug compile time पर, Vue के देखने से पहले, evaluate करता है।

Lines ​

doc.lines में parse की गई range की हर source line के लिए एक entry होती है। वह इसका उत्तर देती है कि "इस line पर क्या है, और उसे कितना गहरा indent होना चाहिए", जो formatter को चाहिए और अकेला tree नहीं बताता।

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
}

इस template के लिए:

pug
ul#list
  li(
    v-for="x in xs"
    :key="x"
  ) {{ x }}
  p.
    some text
Linekinddepth
ul#listnode0
li(node1
v-for="x in xs"attribute2
:key="x"attribute2
) {{ x }}paren-close1
p.node1
some textraw2
(आखिरी line break के बाद)blank0
kindLinedepth
nodenode शुरू करती हैउसकी nesting depth
attributemulti-line attribute block के अंदरtag की depth + 1
paren-closeblock को बंद करने वाले ) से शुरू होती हैtag की depth
rawtext block, comment, code block या filter के अंदरमालिक की depth + 1
continuationऊपर की line को जारी रखती है (multi-line value), या error के बाद छोड़ी गई हैउस line की depth + 1
blankखाली या केवल whitespace0

anchor उन lines पर सेट होता है जिन्हें depth का पालन करने के बजाय किसी दूसरी line के सापेक्ष अपना column रखना होता है: text block की पहली line के बाद की lines (उसी पहली line से anchored), और continuation lines (उस line से anchored जिसे वे जारी रखती हैं)। इस template में

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

two वाली line one वाली line से anchored है, और title value की दूसरी line उसके ऊपर की line से। जब कोई formatter anchor line को हिलाता है, तो वह इन्हें भी उतना ही हिलाता है।

MIT License के अंतर्गत जारी।