Custom method

20 views
Skip to first unread message

Daniel Jipa

unread,
Jun 18, 2015, 7:21:57 AM6/18/15
to rythm...@googlegroups.com
Hello, 
Is there a possibility to create custom methods. I've created a parser just like I18NParser but I still get a method not defined error. Maybe I need to register that parser 
in the application somewhere. 
Is there a sample code available? Google searches bring me no luck.
Thank you

green

unread,
Jun 18, 2015, 7:57:50 AM6/18/15
to rythm...@googlegroups.com
Are you talking about custom directive? Maybe you want to take a look at https://github.com/greenlaw110/play-rythm/tree/master/src/org/rythmengine/play/parsers for sample parser code and also https://github.com/greenlaw110/play-rythm/blob/master/src/org/rythmengine/play/RythmPlugin.java#L523 on how to register those parsers

If you want to create transformers, please refer to http://rythmengine.org/doc/user_defined_transformer.md

--
You received this message because you are subscribed to the Google Groups "rythmengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rythmengine...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Jipa

unread,
Jun 18, 2015, 8:44:51 AM6/18/15
to rythm...@googlegroups.com
I am trying to integrate Rythm in Pippo and for this I need to do a custom method so I can use in the templates. Something like this:

<script src=@webjarsAt("jquery/1.11.1/jquery.min.js")></script>

I made a class WebjarsAtParser extending KeywordParserFactory and also a PippoDialect with buildin classes WebjarsAtParser.class
But when I want to register the parser I get an error:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot find dialect by Id: pippo
at org.rythmengine.internal.dialect.DialectManager.registerExternalParsers(DialectManager.java:134)
at org.rythmengine.internal.ExtensionManager.registerUserDefinedParsers(ExtensionManager.java:84)
at ro.danjee.App.main(App.java:25)

Code looks like this:
Map<String, Object> conf = new HashMap<String, Object>();
Rythm.init(conf);
RythmEngine engine = Rythm.engine();
IParserFactory[] factories = {new WebjarsAtParser()};
engine.extensionManager().registerUserDefinedParsers(factories).registerUserDefinedParsers(PippoDialect.ID, factories);

It seems that DialectManager class has some fixed dialects and I cannot push mine.
I'm pretty sure there is something I miss.

Thank you, 

Daniel 

Daniel Jipa

unread,
Jun 18, 2015, 11:44:58 AM6/18/15
to rythm...@googlegroups.com
I need something like what you do with @def but to be declare în Java code and to be available în all templates. Is this possible?

Daniel Jipa

unread,
Jun 18, 2015, 11:44:58 AM6/18/15
to rythm...@googlegroups.com

green

unread,
Jun 18, 2015, 4:29:17 PM6/18/15
to rythm...@googlegroups.com
Don't understand why you need a dialect. Is registering your parser into existing dialect like what is documented at https://github.com/greenlaw110/play-rythm/blob/master/src/org/rythmengine/play/RythmPlugin.java#L523 okay to you?

Daniel Jipa

unread,
Jun 19, 2015, 6:13:58 PM6/19/15
to rythm...@googlegroups.com

Thank you for your help!
Indeed, I did not need a custom dialect for that. I' be had a second look at the verbatim parser that didn't use dialects.
But I still cannot call a custom method defined in java from template. Is there a documentation for that?
Thanks

> You received this message because you are subscribed to a topic in the Google Groups "rythmengine" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/rythmengine/cTRciR3Fjjw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to rythmengine...@googlegroups.com.

green

unread,
Jun 19, 2015, 6:15:06 PM6/19/15
to rythm...@googlegroups.com
Have you registered your parser? 

Daniel Jipa

unread,
Jun 20, 2015, 1:24:31 AM6/20/15
to rythm...@googlegroups.com

Yes, I have.

Thank you for your help!

green

unread,
Jun 20, 2015, 1:50:06 AM6/20/15
to rythm...@googlegroups.com
Can you please send out your code initializing Rythm Engine?

Daniel Jipa

unread,
Jun 20, 2015, 3:55:08 AM6/20/15
to rythm...@googlegroups.com
Map<String, Object> conf = new HashMap<String, Object>();

conf
.put("home.template", "/home/cory/testrythm/src/main/resources/");

Rythm.init(conf);
RythmEngine engine = Rythm.engine();

engine
.extensionManager().registerUserDefinedParsers(new WebjarsAtParser());

Map<String, Object> params = new HashMap<String, Object>();
params.put("who", "World");
params.put("greeting", "My greetings!");

System.out.println(engine.render("/home/cory/testrythm/src/main/resources/hello.html", params));

And the parser is

public class WebjarsAtParser extends KeywordParserFactory {
   
private static final Pattern P = Pattern.compile(
           
getRegex(), Pattern.DOTALL);

   
private static String getRegex(){
       
return "((@webjarsAt)(\\(\\\"(.*)\\\",\\\"(js|css)\\\"\\))(.*))";
   
}

   
private static String getTestRegex(){
       
return "@webjarsAt(\"test\",\"css\")</h5>\n</body>\n</html>";
   
}

   
public static void main(String[] args) {
       
//@webjarsAt("test","css")</h5></body></html>


        String s = getTestRegex();
       
Matcher m = P.matcher(s);
       
if (m.matches()) {
           
for (int i = 1; i <= m.groupCount(); i++){
               
System.out.println(m.group(i));
           
}
       
}
   
}

   
public IParser create(IContext ctx) {
       
return new ParserBase(ctx) {

           
public Token go() {
               
String remain = remain();
               
Matcher m = P.matcher(remain);
               
if (m.matches()) {
                   
for (int i = 1; i <= m.groupCount(); i++){
                       
System.out.println(i + ">>>" + m.group(i));
                   
}
                    step
(m.group(1).length());
                   
return new CodeToken("p(org.rythmengine.utils.S.raw(\"" +getLine(m.group(3), m.group(4))+"\"));", ctx()).pline();
               
} else {
                   
return null;
               
}
           
}

       
};
   
}

   
private String getLine(String uri, String type){
       
if (type.equals("css")){
           
String ret = "<link href=\"${webjarsAt('"+uri+"')}\" rel=\"stylesheet\">";
//            System.out.println(ret);
            return ret;
       
}else if (type.equals("js")){
           
return "<script src=\"${webjarsAt('"+uri+"')}\"></script>";
       
}
       
return null;
   
}

   
@Override
    public IKeyword keyword() {
       
return ExtendedKeyword.WEBJARSAT;
   
}

   
@Override
    protected String patternStr() {
       
return null;
   
}
}


public enum ExtendedKeyword implements IKeyword{
   
WEBJARSAT, PUBLICAT;

   
private final String s;

   
private ExtendedKeyword() {
       
this.s = name().toLowerCase();
   
}

   
private ExtendedKeyword(String s) {
       
this.s = (null == s) ? name().toLowerCase() : s;
   
}

   
@Override
    public String toString() {
       
return s;
   
}

   
public boolean isRegexp() {
       
return !s.equals(name().toLowerCase());
   
}
}

I still got to work on my parser on the regex part. I got confused with the error because it doesn't gave me a hint that the regex could be invalid, it only told me the 
method is undefined.
Thank you
Reply all
Reply to author
Forward
0 new messages