Parser
1.0.0
ショ和 警告:このプロジェクトは、npmjs.comで"dss"から"@documented-style-sheets/parser"に移行しました
DSS 、文書化されたスタイルシートは、汎用コメントガイドとパーサーです(Ex。CSS、Less、Stylus、SASS、SCSS&JSコメント)。このプロジェクトは、スタイルガイドを生成するために使用されるオブジェクトを生成するために静的ファイル分析と解析を行います。
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には、デフォルトでは、コメントブロックのname 、 description 、 state 、 markupの4つのパーサーが含まれています。新しいパーサーを登録することにより、これらのデフォルトを追加またはオーバーライドできます。これらのデフォルトは、 @デコレーターを使用してそれらを識別するパターンにも従います。このBehaivourを変更して、 dss.detector()に異なるコールバック関数を提供できます。
dss.parser 、探している変数の名前と内容を操作するコールバック関数を期待しています。そのコールバック関数によって返されるものはすべて、Generate 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
} )