Highlighting embedded MarkDown in XML document?

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Highlighting embedded MarkDown in XML document?

Postby majo » Sun Jul 27, 2014 8:20 pm

Hi all.

I am trying to set up RSyntaxTextArea to edit XML files that have Markdown marks and text embedded between certain XML tags. What would be the best way to go about hooking into the XML parser and directing these Markdown blocks into the Markdown parser to get proper highlighting in the Markdown blocks?

I like write own TokenMaker implementation but I like only extend XML definiton of RSyntaxTextArea.
But I don´t now where find XML definiton of RSyntaxTextArea.

So, can sameone give me simple idea and best way how do it right?
majo
 
Posts: 10
Joined: Sun Jul 27, 2014 7:53 pm

Re: Highlighting embedded MarkDown in XML document?

Postby robert » Fri Aug 01, 2014 1:22 pm

This is not possible without creating a custom TokenMaker. You would use XMLTokenMaker.flex as a starting point, and use this wiki article as a starting point.

Good luck!
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: Highlighting embedded MarkDown in XML document?

Postby majo » Sat Aug 02, 2014 9:36 pm

robert wrote:This is not possible without creating a custom TokenMaker. You would use XMLTokenMaker.flex as a starting point, and use this wiki article as a starting point.

Good luck!



Thanks, so when I only add others new start and end marks to this file XMLTokenMaker.flex
and will follow instructions:

once you run this file through JFlex, you'll have to make manual changes to it. Once I stop being lazy, there will be a JFlex skeleton file that handles these changes for you, so the generated .java file will work out-of-the-box, but for now you have to do the following things manually: There are two zzRefill() and yyreset() methods with the same signatures in the generated file. You need to delete the second of each definition (the ones generated by the lexer). Change the declaration/definition of zzBuffer to NOT be initialized. This is a needless memory allocation for us since we will be pointing the array somewhere else anyway.

So i get new Class?? and then can add to my project into source code with follow segmet of code: ?
AbstractTokenMakerFactory atmf = (AbstractTokenMakerFactory)TokenMakerFactory.getDefaultInstance();
atmf.putMapping("text/myLanguage", "MyNewClass");
textArea.setSyntaxEditingStyle("text/myLanguage");
?

When I want still use SyntaxConstants.SYNTAX_STYLE_XML, so can I simply write
atmf.putMapping("text/xml", "MyNewClass");
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);???

And is possible use TokenMakerMaker to create New tokenMaker witch combinate XML and Markdown?
majo
 
Posts: 10
Joined: Sun Jul 27, 2014 7:53 pm

Re: Highlighting embedded MarkDown in XML document?

Postby majo » Mon Aug 04, 2014 3:55 pm

Hi, Robert,
I try use XMLTokenMaker.flex as a starting point. But when use JFlex 1.6 and your XMLTokenMaker.flex I get new Class XMLTokenMaker.class .. then remove second definitions of both zzRefill and yyreset method and finally also change the declaration/definition of zzBuffer to NOT be initialized .

and set new language for RSyntaxTextArea it still forezen when I try write atrribute for XML element som an only for example write:
Code: Select all
<test></test> - it´s OK
<test first=></test> - it´s still OK
<test first=></test>  and when a try type " it forzen


Any idea where is problem?
majo
 
Posts: 10
Joined: Sun Jul 27, 2014 7:53 pm

Re: Highlighting embedded MarkDown in XML document?

Postby robert » Tue Aug 05, 2014 2:46 am

I'd really have to see your TokenMaker class to figure out what's wrong.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: Highlighting embedded MarkDown in XML document?

Postby Guest » Thu Aug 07, 2014 6:31 pm

Hi robert

I solved problem with forzen RSTA, I use older version of JFlex (1.4.1) and it start working.

Then I try add into your definition XMLTokenMaker new mark, and hawe little problem. Can you help me with it?See picture, please.
Image



<YYINITIAL> {
"<!--" { start = zzStartRead; prevState = zzLexicalState; yybegin(COMMENT); }
{CDataBegin} { addToken(Token.MARKUP_CDATA_DELIMITER); start = zzMarkedPos; yybegin(CDATA); }
"<!" { start = zzMarkedPos-2; inInternalDtd = false; yybegin(DTD); }
"<?" { start = zzMarkedPos-2; yybegin(PI); }
"<"{TagName} {
int count = yylength();
addToken(zzStartRead,zzStartRead, Token.MARKUP_TAG_DELIMITER);
addToken(zzMarkedPos-(count-1), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
yybegin(INTAG);
}

"#"{TagName} {
int count = yylength();
addToken(zzStartRead,zzStartRead, Token.MARKUP_TAG_DELIMITER);
addToken(zzMarkedPos-(count-1), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
yybegin(INTAG);
}

"</"{TagName} {
int count = yylength();
addToken(zzStartRead,zzStartRead+1, Token.MARKUP_TAG_DELIMITER);
addToken(zzMarkedPos-(count-2), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
yybegin(INTAG);
}
"##"{TagName} {
int count = yylength();
addToken(zzStartRead,zzStartRead+1, Token.MARKUP_TAG_DELIMITER);
addToken(zzMarkedPos-(count-2), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
yybegin(INTAG);
}

"<" { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
"#" { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
"##" { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
"###" { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }

"</" { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
{LineTerminator} { addNullToken(); return firstToken; }
{Identifier} { addToken(Token.IDENTIFIER); }
{EntityReference} { addToken(Token.MARKUP_ENTITY_REFERENCE); }
{Whitespace}+ { addToken(Token.WHITESPACE); }
<<EOF>> { addNullToken(); return firstToken; }
}

<COMMENT> {
[^hwf\n\-]+ {}
{URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.MARKUP_COMMENT); addHyperlinkToken(temp,zzMarkedPos-1, Token.MARKUP_COMMENT); start = zzMarkedPos; }
[hwf] {}
"-->" { int temp = zzMarkedPos; addToken(start,zzStartRead+2, Token.MARKUP_COMMENT); start = temp; yybegin(prevState); }
"-" {}
{LineTerminator} |
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_COMMENT); addEndToken(INTERNAL_IN_XML_COMMENT - prevState); return firstToken; }
}

<PI> {
[^\n\?]+ {}
{LineTerminator} { addToken(start,zzStartRead-1, Token.MARKUP_PROCESSING_INSTRUCTION); return firstToken; }
"?>" { yybegin(YYINITIAL); addToken(start,zzStartRead+1, Token.MARKUP_PROCESSING_INSTRUCTION); }
"?" {}
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_PROCESSING_INSTRUCTION); return firstToken; }
}

<DTD> {
[^\n\[\]<>]+ {}
"<!--" { int temp = zzStartRead; addToken(start,zzStartRead-1, Token.MARKUP_DTD); start = temp; prevState = zzLexicalState; yybegin(COMMENT); }
"<" {}
"[" { inInternalDtd = true; }
"]" { inInternalDtd = false; }
">" { if (!inInternalDtd) { yybegin(YYINITIAL); addToken(start,zzStartRead, Token.MARKUP_DTD); } }
{LineTerminator} |
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_DTD); addEndToken(inInternalDtd ? INTERNAL_DTD_INTERNAL : INTERNAL_DTD); return firstToken; }
}

<INTAG> {
{InTagIdentifier} { addToken(Token.MARKUP_TAG_ATTRIBUTE); }
{Whitespace}+ { addToken(Token.WHITESPACE); }
"=" { addToken(Token.OPERATOR); }
"/" { addToken(Token.MARKUP_TAG_DELIMITER); /* Not valid but we'll still accept it */ }
"/>" { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
">" { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
"/#" { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
"/##" { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
"/###" { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }

[\"] { start = zzMarkedPos-1; yybegin(INATTR_DOUBLE); }
[\'] { start = zzMarkedPos-1; yybegin(INATTR_SINGLE); }
<<EOF>> { addToken(start,zzStartRead-1, INTERNAL_INTAG); return firstToken; }
}

<INATTR_DOUBLE> {
[^\"]* {}
[\"] { yybegin(INTAG); addToken(start,zzStartRead, Token.MARKUP_TAG_ATTRIBUTE_VALUE); }
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_TAG_ATTRIBUTE_VALUE); addEndToken(INTERNAL_ATTR_DOUBLE); return firstToken; }
}

<INATTR_SINGLE> {
[^\']* {}
[\'] { yybegin(INTAG); addToken(start,zzStartRead, Token.MARKUP_TAG_ATTRIBUTE_VALUE); }
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_TAG_ATTRIBUTE_VALUE); addEndToken(INTERNAL_ATTR_SINGLE); return firstToken; }
}

<CDATA> {
[^\]]+ {}
{CDataEnd} { int temp=zzStartRead; yybegin(YYINITIAL); addToken(start,zzStartRead-1, Token.MARKUP_CDATA); addToken(temp,zzMarkedPos-1, Token.MARKUP_CDATA_DELIMITER); }
"]" {}
<<EOF>> { addToken(start,zzStartRead-1, Token.MARKUP_CDATA); return firstToken; }
}

Code: Select all
<YYINITIAL> {
   "<!--"                     { start = zzStartRead; prevState = zzLexicalState; yybegin(COMMENT); }
   {CDataBegin}               { addToken(Token.MARKUP_CDATA_DELIMITER); start = zzMarkedPos; yybegin(CDATA); }
   "<!"                     { start = zzMarkedPos-2; inInternalDtd = false; yybegin(DTD); }
   "<?"                     { start = zzMarkedPos-2; yybegin(PI); }
   "<"{TagName}            {
                           int count = yylength();
                           addToken(zzStartRead,zzStartRead, Token.MARKUP_TAG_DELIMITER);
                           addToken(zzMarkedPos-(count-1), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
                           yybegin(INTAG);
                        }

   "#"{TagName}            {
                               int count = yylength();
                               addToken(zzStartRead,zzStartRead, Token.MARKUP_TAG_DELIMITER);
                               addToken(zzMarkedPos-(count-1), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
                               yybegin(INTAG);
                            }
   "</"{TagName}            {
                           int count = yylength();
                           addToken(zzStartRead,zzStartRead+1, Token.MARKUP_TAG_DELIMITER);
                           addToken(zzMarkedPos-(count-2), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
                           yybegin(INTAG);
                        }
   "##"{TagName}            {
                               int count = yylength();
                               addToken(zzStartRead,zzStartRead+1, Token.MARKUP_TAG_DELIMITER);
                               addToken(zzMarkedPos-(count-2), zzMarkedPos-1, Token.MARKUP_TAG_NAME);
                               yybegin(INTAG);
                            }
   "<"                     { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
   "#"                      { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
   "##"                      { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
   "###"                      { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
   "</"                  { addToken(Token.MARKUP_TAG_DELIMITER); yybegin(INTAG); }
   {LineTerminator}            { addNullToken(); return firstToken; }
   {Identifier}               { addToken(Token.IDENTIFIER); }
   {EntityReference}            { addToken(Token.MARKUP_ENTITY_REFERENCE); }
   {Whitespace}+               { addToken(Token.WHITESPACE); }
   <<EOF>>                  { addNullToken(); return firstToken; }
}

<COMMENT> {
   [^hwf\n\-]+               {}
   {URL}                  { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.MARKUP_COMMENT); addHyperlinkToken(temp,zzMarkedPos-1, Token.MARKUP_COMMENT); start = zzMarkedPos; }
   [hwf]                  {}
   "-->"                  { int temp = zzMarkedPos; addToken(start,zzStartRead+2, Token.MARKUP_COMMENT); start = temp; yybegin(prevState); }
   "-"                     {}
   {LineTerminator} |
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_COMMENT); addEndToken(INTERNAL_IN_XML_COMMENT - prevState); return firstToken; }
}

<PI> {
   [^\n\?]+                  {}
   {LineTerminator}            { addToken(start,zzStartRead-1, Token.MARKUP_PROCESSING_INSTRUCTION); return firstToken; }
   "?>"                     { yybegin(YYINITIAL); addToken(start,zzStartRead+1, Token.MARKUP_PROCESSING_INSTRUCTION); }
   "?"                     {}
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_PROCESSING_INSTRUCTION); return firstToken; }
}

<DTD> {
   [^\n\[\]<>]+            {}
   "<!--"                  { int temp = zzStartRead; addToken(start,zzStartRead-1, Token.MARKUP_DTD); start = temp; prevState = zzLexicalState; yybegin(COMMENT); }
   "<"                     {}
   "["                     { inInternalDtd = true; }
   "]"                     { inInternalDtd = false; }
   ">"                     { if (!inInternalDtd) { yybegin(YYINITIAL); addToken(start,zzStartRead, Token.MARKUP_DTD); } }
   {LineTerminator} |
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_DTD); addEndToken(inInternalDtd ? INTERNAL_DTD_INTERNAL : INTERNAL_DTD); return firstToken; }
}

<INTAG> {
   {InTagIdentifier}            { addToken(Token.MARKUP_TAG_ATTRIBUTE); }
   {Whitespace}+               { addToken(Token.WHITESPACE); }
   "="                     { addToken(Token.OPERATOR); }
   "/"                     { addToken(Token.MARKUP_TAG_DELIMITER); /* Not valid but we'll still accept it */ }
   "/>"                  { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
   ">"                     { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
   "/#"                     { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
   "/##"                     { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
   "/###"                     { yybegin(YYINITIAL); addToken(Token.MARKUP_TAG_DELIMITER); }
   [\"]                  { start = zzMarkedPos-1; yybegin(INATTR_DOUBLE); }
   [\']                  { start = zzMarkedPos-1; yybegin(INATTR_SINGLE); }
   <<EOF>>                  { addToken(start,zzStartRead-1, INTERNAL_INTAG); return firstToken; }
}

<INATTR_DOUBLE> {
   [^\"]*                  {}
   [\"]                  { yybegin(INTAG); addToken(start,zzStartRead, Token.MARKUP_TAG_ATTRIBUTE_VALUE); }
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_TAG_ATTRIBUTE_VALUE); addEndToken(INTERNAL_ATTR_DOUBLE); return firstToken; }
}

<INATTR_SINGLE> {
   [^\']*                  {}
   [\']                  { yybegin(INTAG); addToken(start,zzStartRead, Token.MARKUP_TAG_ATTRIBUTE_VALUE); }
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_TAG_ATTRIBUTE_VALUE); addEndToken(INTERNAL_ATTR_SINGLE); return firstToken; }
}

<CDATA> {
   [^\]]+                  {}
   {CDataEnd}               { int temp=zzStartRead; yybegin(YYINITIAL); addToken(start,zzStartRead-1, Token.MARKUP_CDATA); addToken(temp,zzMarkedPos-1, Token.MARKUP_CDATA_DELIMITER); }
   "]"                     {}
   <<EOF>>                  { addToken(start,zzStartRead-1, Token.MARKUP_CDATA); return firstToken; }
}
Guest
 

Re: Highlighting embedded MarkDown in XML document?

Postby majo » Thu Aug 07, 2014 10:24 pm

I solved highlighting for my new marks for H1 - H6
majo
 
Posts: 10
Joined: Sun Jul 27, 2014 7:53 pm

Re: Highlighting embedded MarkDown in XML document?

Postby majo » Fri Aug 08, 2014 10:56 am

Hi Robert,
How can set my own color for my own marks and own colour for Name of my marks??

I like add my own token types to package org.fife.ui.rsyntaxtextarea -> public interface TokenTypes, but I don´t know where can define color for my TokenTypes
majo
 
Posts: 10
Joined: Sun Jul 27, 2014 7:53 pm

Re: Highlighting embedded MarkDown in XML document?

Postby robert » Mon Aug 11, 2014 11:58 am

To add default colors for your new token types, you'll have to edit the org.fife.ui.rsyntaxtextarea.SyntaxScheme class. Alternatively, if you only create one instance of RSyntaxTextArea in your application, you can modify its SyntaxScheme like so:

java code:

SyntaxScheme scheme = textArea.getSyntaxScheme();
scheme.getStyle(MY_NEW_STYLE_CONSTANT).foreground = Color.red;
scheme.getStyle(MY_NEW_STYLE_CONSTANT_2).foreground = Color.blue;
... etc. ...


Each org.fife.ui.rsyntaxtextarea.Style in the SyntaxScheme controls the font, foreground, and background for that token type.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron