Parser
1.0.0
配x 警告:该项目已在npmjs.com上从"dss"转移到"@documented-style-sheets/parser"
DSS ,有记录的样式表是通用评论指南和解析器(例如,CSS,LISNES,手写笔,SASS,SASS,SCSS&JS评论)。该项目进行静态文件分析和解析以生成用于生成styleGuides的对象。
dss.detectordss.parsernpm i @documented-style-sheets/parser在大多数情况下,您将需要将DSS解析器包含在构建步骤中,该步骤将自动生成文档文件(或者您只想使用此返回的Object来播放以获取其他方式);无论哪种方式,我们都会正式支持Grunt插件和Gulp插件。
//
// @name Button
// @description Your standard form button.
//
// @state :hover - Highlights when hovering.
// @state :disabled - Dims the button when disabled.
// @state .primary - Indicates button is the primary action.
// @state .smaller - A smaller button
//
// @markup
// <button>This is a button</button>
// // Requirements
const fs = require ( 'fs' )
const { parse } = require ( '@documented-style-sheets/parser' )
// Get file contents
const fileContents = fs . readFileSync ( 'styles.css' )
// Run the DSS Parser on the file contents
parse ( fileContents , { } , function ( parsedObject ) {
// Output the parsed document
console . log ( parsedObject )
} ) {
"name" : " Button " ,
"description" : " Your standard form button. " ,
"state" : [
{
"name" : " :hover " ,
"escaped" : " pseudo-class-hover " ,
"description" : " Highlights when hovering. "
},
{
"name" : " :disabled " ,
"escaped" : " pseudo-class-disabled " ,
"description" : " Dims the button when disabled. "
},
{
"name" : " .primary " ,
"escaped" : " primary " ,
"description" : " Indicates button is the primary action. "
},
{
"name" : " .smaller " ,
"escaped" : " smaller " ,
"description" : " A smaller button "
}
],
"markup" : {
"example" : " <button>This is a button</button> " ,
"escaped" : " <button>This is a button</button> "
}
}该方法定义了在文本行中找到感兴趣点(即变量)的方式,然后被解析。 DSS Dogfoods此API和默认实现如下所示。
// Describe default detection pattern
// Note: the current line, as a string, is passed to this function
const dss = require ( '@documented-style-sheets/parser' )
dss . detector ( function ( line ) {
if ( typeof line !== 'string' ) {
return false
}
var reference = line . split ( "nn" ) . pop ( )
return ! ! reference . match ( / .*@ / )
} )
dss . parse ( ... ) 默认情况下, DSS包括4个解析器,用于评论块的name , description , state和markup 。您可以通过注册新解析器来添加或覆盖这些默认值。这些默认值还遵循一种模式,该模式使用@ Decorator来识别它们。您可以修改此BehaiVour,为dss.detector()提供不同的回调函数。
dss.parser期望您要寻找的变量的名称和一个回调功能来操纵内容。该回调函数返回的任何内容都是生成JSON中使用的内容。
this :this.file :当前文件this.name :解析器的名称this.options :最初传递给dss.parse()选项this.line :this.line.contents :与此变量关联的内容this.line.from :找到此变量的行号this.line.to :该变量内容结束的行号this.block :this.block.contents :与此变量的评论块相关的内容this.block.from :该变量的评论块启动的行号this.block.to :该变量的评论块结束的行号 // Matches @version
dss . parser ( 'version' , function ( ) {
// Just returns the lines contents
return this . line . contents
} ) dss . parser ( 'link' , function ( ) {
// Replace link with HTML wrapped version
const exp = / (b(https?|ftp|file): / / [ - A - Z0 - 9 + & @# / % ? = ~ _ | ! : , . ; ] * [ - A - Z0 - 9 + & @# / %= ~ _ | ] ) / ig
this . line . contents . replace ( exp , "<a href='$1'>$1</a>" )
return line
} )