Edit pre tags with sitemesh

View: New views
2 Messages — Rating Filter:   Alert me  

Edit pre tags with sitemesh

by sheeep :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I don't know if this is the right place to post this, but i'll give it a try:)

I just downloaded sitemesh and looked over some examples (the very few I could find).
I have to edit the pre tags, from a jsp or html page, that contain java code. (make the comments italic, keywords red), but
I am not quite sure how to approach this with sitemesh. Any help would be much appreciated

Thank u so much in advance

Re: Edit pre tags with sitemesh

by Joe Walnes-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Sheeep,

SiteMesh does have the ability to edit contents of tags on the page, but it's an advanced feature.

----

Before I dive into how to do it, you might want to consider some client side JavaScript libraries that would be much simpler to use:


----

If you'd prefer to do the changes server side, then here's how:

* Firstly, I'd recommend SiteMesh 3 for this. Although it hasn't officially been released yet, the API has had an overhaul which makes it much easier to do this kind of stuff. The docs and download are here: http://www.sitemesh.org/

* You need to create a TagRule class that will be assigned to the <pre> tag. This will be called by SiteMesh as it's processing any page, and gives you the opportunity for manipulation.

public class CodeFormattingTagRule extends BasicBlockRule<String> {

  /** Called when SiteMesh encounters the start tag (i.e. <pre>) */
  @Override
  protected abstract String processStart(Tag tag) {
    // Get <pre language="XXX"> attribute from page. May be null if no attribute.
    String language = tag.getAttributeValue("language", false);

    // Write the opening <pre> tag (including it's attributes, back to the page.
    tag.writeTo(tagProcessorContext.currentBuffer());

    if (shouldFormatLanguage(language)) {
      // Create a new buffer, so the contents of the tag do not get written to the page.
      tagProcessorContext.pushBuffer();  
    }

    // Anything can be returned from this method, which will then be passed back
    // in to the processEnd() method. You can use this for keeping track of state.
    // To use a type other than String, change the generic type parameter of the
    // class.
    return language; 
  }

  /** Called when SiteMesh encounters the end tag (i.e. </pre>) */
  @Override
  protected void processEnd(Tag tag, String language) {
    if (shouldFormatLanguage(language) {
      // Fetch the contents of the buffer, that has was created since <pre>
      // was encountered. (i.e. the code in between the <pre>…</pre> tags).
      String code = tagProcessorContext.currentBufferContents().toString();
      // End the temporary buffer. All further content will get written back to the page.
      tagProcessorContext.popBuffer();
      // Format the code.
      String formattedCode = formatCode(language, code);
      // Write the formatted code to the page.
      tagProcessorContext.currentBuffer().append(formattedCode);
    }
    // Write the </pre> tag back out to the page.
    tag.writeTo(tagProcessorContext.currentBuffer());
  }

  // Plug in your actual formatting code here....

  private boolean shouldFormatLanguage(String language) {
    ...
  }

  private String formatCode(String language, String code) {
    ...
  }

}

* You create a TagRuleBundle class, which tells SM how to install your rule into the parser.

public class CodeFormattingTagRuleBundle implements TagRuleBundle {
 
  @Override
  public void install(State page, ContentProperty contentProperty, SiteMeshContext context) {
    page.addRule("pre", new CodeFormattingTagRule());
  }
 
  @Override
  public void cleanUp(State page, ContentProperty contentProperty, SiteMeshContext context) {
    // Nothing needed.
  }
 
}

* Finally, you need to let SM know about your TagRuleBundle. See the last section of http://www.sitemesh.org/configuration.html

thanks
-Joe

On Tue, Oct 27, 2009 at 7:24 AM, sheeep <blackwidow6x@...> wrote:

Hello,

I don't know if this is the right place to post this, but i'll give it a
try:)

I just downloaded sitemesh and looked over some examples (the very few I
could find).
I have to edit the pre tags, from a jsp or html page, that contain java
code. (make the comments italic, keywords red), but
I am not quite sure how to approach this with sitemesh. Any help would be
much appreciated :-)

Thank u so much in advance
--
View this message in context: http://www.nabble.com/Edit-pre-tags-with-sitemesh-tp26076735p26076735.html
Sent from the OpenSymphony - SiteMesh mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...