我正在写一个.tmLanguage文件来突出显示markdown文件(.md),并且markdown文件中有一些cpp代码。我想使用c ++。tmLanguage(在Packages \ C ++中)来解决语法问题。我该怎么办?
答案 0 :(得分:1)
学习如何编写语言定义的最佳方法是查看现有语言定义。包控制中存在plenty现有的Markdown语法定义,因此我建议您仔细查看它们的结构。
假设您使用PackageDev
的.YAML-tmLanguage
格式,受保护代码的存储库将如下所示:
repository:
fenced-c:
name: markup.raw.block.markdown markup.raw.block.fenced.markdown
begin: (\s*```)\s*(c)\s*$
end: (\1)\n
captures:
'1': {name: punctuation.definition.fenced.markdown}
'2': {name: variable.language.fenced.markdown}
patterns:
- include: source.c
fenced-c++:
name: markup.raw.block.markdown markup.raw.block.fenced.markdown
begin: (\s*```)\s*(c\+\+)\s*$
end: (\1)\n
captures:
'1': {name: punctuation.definition.fenced.markdown}
'2': {name: variable.language.fenced.markdown}
patterns:
- include: source.c++
fenced-coffee:
name: markup.raw.block.markdown markup.raw.block.fenced.markdown
begin: (\s*```)\s*(coffee)\s*$
end: (\1)\n
captures:
'1': {name: punctuation.definition.fenced.markdown}
'2': {name: variable.language.fenced.markdown}
patterns:
- include: source.coffee
# etc.
使用内置转换工具转换为XML,如下所示:
<key>repository</key>
<dict>
<key>fenced-c</key>
<dict>
<key>begin</key>
<string>(\s*```)\s*(c)\s*$</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.fenced.markdown</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>variable.language.fenced.markdown</string>
</dict>
</dict>
<key>end</key>
<string>(\1)\n</string>
<key>name</key>
<string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>source.c</string>
</dict>
</array>
</dict>
<key>fenced-c++</key>
<dict>
<key>begin</key>
<string>(\s*```)\s*(c\+\+)\s*$</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.fenced.markdown</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>variable.language.fenced.markdown</string>
</dict>
</dict>
<key>end</key>
<string>(\1)\n</string>
<key>name</key>
<string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>source.c++</string>
</dict>
</array>
</dict>
<key>fenced-coffee</key>
<dict>
<key>begin</key>
<string>(\s*```)\s*(coffee)\s*$</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.fenced.markdown</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>variable.language.fenced.markdown</string>
</dict>
</dict>
<key>end</key>
<string>(\1)\n</string>
<key>name</key>
<string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>source.coffee</string>
</dict>
</array>
</dict>
<!-- and so on... -->
我建议您使用PackageDev
,因为YAML 所以比XML更容易使用。或者,您可以使用新的基于YAML的.sublime-syntax
格式,但目前仅在Sublime Text 3 development builds 3084及更高版本中支持,这意味着您需要成为注册用户才能访问它。我建议暂时坚持使用.YAML-tmLanguage
,直到发布支持新格式的新public build为止。
您可以查看PackageDev
Github repo以查找.YAML-tmLanguage
个文件的更多示例。另外,我是Python Improved
项目的首席开发人员,您也可以找到我的所有源文件on Github。