Troubles with building a syntax file
See original GitHub issueI’m attempting to write a syntax file for Laravel Blade to contribute, and I am coming up against a wall trying to get single word directives like @continue
to highlight, which seems like it should be simple enough.
Additionally there are more complex directives like @for($i = 0; $i < 2; $i++)
which I want highlighted and their contents treated like PHP.
I’ve posted a question on SO, but figured it was pretty niche and posting here might help get some eyeballs on it.
And here’s the code I have so far, in case someone doesn’t like external links:
export default function(hljs) {
const EXPRESSION_DIRECTIVES = {
$pattern: /@[a-zA-Z]+/,
'template-tag': [
'case', 'each', 'elseif', 'empty', 'env', 'error', 'extends', 'for', 'foreach', 'forelse',
'hasSection', 'if', 'include', 'includeFirst', 'includeIf', 'includeUnless', 'includeWhen',
'inject', 'isset', 'method', 'prepend', 'section', 'sectionMissing', 'stack', 'switch',
'unless', 'while', 'yield'
].map(v => '@' + v).join(" ")
};
const STANDALONE_DIRECTIVES = {
$pattern: /@[a-zA-Z]+/,
'template-tag': [
'auth', 'break', 'continue', 'csrf', 'default', 'else', 'endauth', 'endempty', 'endenv',
'enderror', 'endfor', 'endforeach', 'endforelse', 'endguest', 'endif', 'endisset', 'endphp',
'endprepend', 'endproduction', 'endpush', 'endsection', 'endswitch', 'endunless', 'endwhile',
'guest', 'once', 'parent', 'php', 'production', 'props', 'push', 'show'
].map(v => '@' + v).join(" ")
};
return {
name: 'Blade',
case_insensitive: true,
subLanguage: 'xml',
contains: [
hljs.COMMENT(/\{\{--/, /--\}\}/),
// output with HTML escaping
{
className: 'template-variable',
begin: /\{\{/,
starts: {
end: /\}\}/,
returnEnd: true,
subLanguage: 'php',
},
},
{
className: 'template-variable',
begin: /\}\}/,
},
// output with no HTML escaping
{
className: 'template-variable',
begin: /\{!!/,
starts: {
end: /!!\}/,
returnEnd: true,
subLanguage: 'php',
},
},
{
className: 'template-variable',
begin: /!!\}/,
},
// directly inserted PHP code
{
className: 'template-tag',
begin: /@php/,
starts: {
end: /@endphp/,
returnEnd: true,
subLanguage: 'php',
},
relevance: 10
},
// directives with arguments like @yield('foo')
{
begin: /@([a-zA-Z]+)\s*\(/,
keywords: EXPRESSION_DIRECTIVES,
starts: {
end: /\)/,
returnEnd: true,
subLanguage: 'php'
}
},
// directives without arguments like @section
{
begin: /@([a-zA-Z]+)/,
keywords: STANDALONE_DIRECTIVES,
},
]
};
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Creating a syntax file - YouTube
SPSS Syntax files ; how to open them and write notes to yourself within the program.
Read more >SPSS Tutorials: Using SPSS Syntax - LibGuides
Opening Syntax Files. To open a syntax file on your computer, click File > Open > Syntax. You can do this from any...
Read more >7.3. Catching Ant Build File Syntax Problems - O'Reilly
You have a problem with a build file, and you would like to track it down. Solution. The current version of Ant displays...
Read more >A case against syntax highlighting - Linus Åkesson
Syntax highlighting can show you, for example, that you are using library subroutine name as variable. And, in C, this can be a...
Read more >Problem with the FACTOR command in syntax - Forums - IBM
I currently use SPSS for Mac v21 and v22. I have successfully used SPSS syntax to transform variables and analyze data for many...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Those relevances are pretty scary if you ever plan to participate in auto-detection. Most typically one should not use relevance at all (other than to set it to 0 for many things). The only thing I see here that might deserve a 10 is
@php
- though@phpend
would be far better to give the points too, since it’s longer and more specific. Your real relevance will come from your sublanguages as they match tokens.Example:
@section
is a perfectly valid variable in Ruby with a relevance of 1.That does not work because @ isn’t in the default
$pattern
. (so no keyword with @ will ever match) The default is\w+
(word like characters)… so “blah_blah” is a potential keyword, “@blah” is not.You may want to checkout:
$pattern
- https://highlightjs.readthedocs.io/en/latest/mode-reference.html#lexemes-now-keywords-patternbeginKeywords
might also help you - it’s syntactic sugar to write a large(a|b|c|d)
rule which can avoid the need for $pattern