PHP and HTML Highlighting #130

Open
opened 2017-10-11 03:54:14 -05:00 by Kabennsky · 1 comment
Kabennsky commented 2017-10-11 03:54:14 -05:00 (Migrated from github.com)

Has someone ever tried to Highlighting PHP and HTML Tags in the Editor?
Our programmers often have to handle PHP-Files with contains PHP- and HTML-Code...
I tried making a new xshd merging the PHP and the HTML xshd files. But i don't get it to work right..
Someone has experience with that?
Or maybe can someone explain how the xshd will be interpreted?
What happens when two rules try to colorize text? Will the first or the last be taken?

Example:
<html>
This should be colorized with some HTML color

<?php>
This should be colorized with some PHP color

Has someone ever tried to Highlighting PHP and HTML Tags in the Editor? Our programmers often have to handle PHP-Files with contains PHP- and HTML-Code... I tried making a new xshd merging the PHP and the HTML xshd files. But i don't get it to work right.. Someone has experience with that? Or maybe can someone explain how the xshd will be interpreted? What happens when two rules try to colorize text? Will the first or the last be taken? Example: \<html\> This should be colorized with some HTML color \<?php\> This should be colorized with some PHP color
siegfriedpammer commented 2020-02-23 05:20:00 -06:00 (Migrated from github.com)

This should work:

<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
	<!-- This is the main ruleset. -->
	<RuleSet ignoreCase="true">
		<Span begin="&lt;\?php" end="\?&gt;" foreground="Red" fontWeight="bold" ruleSet="PHP" />
		<Import ruleSet="HTML/" />
	</RuleSet>

	<RuleSet name="PHP">
		<Import ruleSet="PHP/" />
	</RuleSet>
</SyntaxDefinition>

Or maybe can someone explain how the xshd will be interpreted?
What happens when two rules try to colorize text? Will the first or the last be taken?

As far as I can see, the rules will be interpreted in document order and the most suitable (i.e. longest match) will be chosen.

In the above example we first import the existing main rulesets of the HTML and PHP highlighting definitions into our main ruleset (HTML) and we create an extra local ruleset for the PHP rules.

Then we create a new span that matches <?php as begin expression and ?> as end expression. Note that the begin and end expressions are evaluated as regular expressions, therefore we have to escape the question marks. We then use the newly created "PHP" ruleset inside the span.

It is important that the PHP span highlighting rule comes before any of the HTML rules, so that the highlighting engine picks it up first.

Hope this helps!

This should work: ```xml <?xml version="1.0"?> <SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008"> <!-- This is the main ruleset. --> <RuleSet ignoreCase="true"> <Span begin="&lt;\?php" end="\?&gt;" foreground="Red" fontWeight="bold" ruleSet="PHP" /> <Import ruleSet="HTML/" /> </RuleSet> <RuleSet name="PHP"> <Import ruleSet="PHP/" /> </RuleSet> </SyntaxDefinition> ``` > Or maybe can someone explain how the xshd will be interpreted? What happens when two rules try to colorize text? Will the first or the last be taken? As far as I can see, the rules will be interpreted in document order and the most suitable (i.e. longest match) will be chosen. In the above example we first import the existing main rulesets of the `HTML` and `PHP` highlighting definitions into our main ruleset (HTML) and we create an extra local ruleset for the PHP rules. Then we create a new span that matches `<?php` as begin expression and `?>` as end expression. Note that the begin and end expressions are evaluated as regular expressions, therefore we have to escape the question marks. We then use the newly created "PHP" ruleset inside the span. It is important that the PHP span highlighting rule comes before any of the HTML rules, so that the highlighting engine picks it up first. Hope this helps!
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-icsharpcode/AvalonEdit#130
No description provided.