Re: Edit pre tags with sitemesh

Joe Walnes <[email protected]> Sun, 1 Nov 2009 06:56:36 -0500
Newsgroups gmane.comp.web.sitemesh.general
Message-ID <[email protected]>
--001485f5b0c0fd323004774df378
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

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 sid=
e
JavaScript libraries that would be much simpler to use:

- http://alexgorbatchev.com/wiki/SyntaxHighlighter
- http://code.google.com/p/google-code-prettify/
-
http://www.webdesignbooth.com/9-useful-javascript-syntax-highlighting-scrip=
ts/

----

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 t=
o
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=3D"XXX"> attribute from page. May be null if no
attribute.
    String language =3D 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 o=
f
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>=85</pre> tags=
).
      String code =3D tagProcessorContext.currentBufferContents().toString(=
);
      // End the temporary buffer. All further content will get written bac=
k
to the page.
      tagProcessorContext.popBuffer();
      // Format the code.
      String formattedCode =3D 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 <[email protected]> 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.htm=
l
> Sent from the OpenSymphony - SiteMesh mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

--001485f5b0c0fd323004774df378
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

Hi Sheeep,<div><br></div><div>SiteMesh does have the ability to edit conten=
ts of tags on the page, but it&#39;s an advanced feature.</div><div><br></d=
iv><div>----</div><div><br></div><div>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:</div>
<div><br></div><div>-=A0<a href=3D"http://alexgorbatchev.com/wiki/SyntaxHig=
hlighter">http://alexgorbatchev.com/wiki/SyntaxHighlighter</a></div><div>- =
<a href=3D"http://code.google.com/p/google-code-prettify/">http://code.goog=
le.com/p/google-code-prettify/</a></div>
<div>-=A0<a href=3D"http://www.webdesignbooth.com/9-useful-javascript-synta=
x-highlighting-scripts/">http://www.webdesignbooth.com/9-useful-javascript-=
syntax-highlighting-scripts/</a></div><div><br></div><div>----</div><div><b=
r>
</div><div>If you&#39;d prefer to do the changes server side, then here&#39=
;s how:</div><div><br></div><div>* Firstly, I&#39;d recommend SiteMesh 3 fo=
r this. Although it hasn&#39;t officially been released yet, the API has ha=
d an overhaul which makes it much easier to do this kind of stuff. The docs=
 and download are here:=A0<a href=3D"http://www.sitemesh.org/">http://www.s=
itemesh.org/</a></div>
<div><br></div><div>* You need to create a TagRule class that will be assig=
ned to the &lt;pre&gt; tag. This will be called by SiteMesh as it&#39;s pro=
cessing any page, and gives you the opportunity for manipulation.<br><br>
</div><div><div>public class CodeFormattingTagRule extends BasicBlockRule&l=
t;String&gt; {</div><div><br></div><div>=A0=A0/** Called when SiteMesh enco=
unters the start tag (i.e. &lt;pre&gt;) */</div><div>=A0=A0@Override</div><=
div>
=A0=A0protected abstract String processStart(Tag tag) {</div><div>=A0=A0 =
=A0// Get &lt;pre language=3D&quot;XXX&quot;&gt; attribute from page. May b=
e null if no attribute.</div><div>=A0=A0 =A0String language =3D tag.getAttr=
ibuteValue(&quot;language&quot;, false);</div>
<div><br></div><div>=A0=A0 =A0// Write the opening &lt;pre&gt; tag (includi=
ng it&#39;s attributes, back to the page.</div><div>=A0=A0 =A0tag.writeTo(t=
agProcessorContext.currentBuffer());</div><div><br></div><div>=A0=A0 =A0if =
(shouldFormatLanguage(language)) {</div>
<div>=A0=A0 =A0 =A0// Create a new buffer, so the contents of the tag do no=
t get written to the page.</div><div>=A0=A0 =A0 =A0tagProcessorContext.push=
Buffer(); =A0</div><div>=A0=A0 =A0}</div><div><br></div><div>=A0=A0 =A0// A=
nything can be returned from this method, which will then be passed back</d=
iv>
<div>=A0=A0 =A0// in to the processEnd() method. You can use this for keepi=
ng track of state.</div><div>=A0=A0 =A0// To use a type other than String, =
change the generic type parameter of the</div><div>=A0=A0 =A0// class.</div=
><div>=A0=A0 =A0return language;=A0</div>
<div>=A0=A0}</div><div><br></div><div>=A0=A0/** Called when SiteMesh encoun=
ters the end tag (i.e. &lt;/pre&gt;) */</div><div>=A0=A0@Override</div><div=
>=A0=A0protected void processEnd(Tag tag, String language) {</div><div>=A0=
=A0 =A0if (shouldFormatLanguage(language) {</div>
<div>=A0=A0 =A0 =A0// Fetch the contents of the buffer, that has was create=
d since &lt;pre&gt;</div><div>=A0=A0 =A0 =A0// was encountered. (i.e. the c=
ode in between the &lt;pre&gt;=85&lt;/pre&gt; tags).</div><div>=A0=A0 =A0 =
=A0String code =3D tagProcessorContext.currentBufferContents().toString();<=
/div>
<div>=A0=A0 =A0 =A0// End the temporary buffer. All further content will ge=
t written back to the page.</div><div>=A0=A0 =A0 =A0tagProcessorContext.pop=
Buffer();</div><div>=A0=A0 =A0 =A0// Format the code.</div><div>=A0=A0 =A0 =
=A0String formattedCode =3D formatCode(language, code);</div>
<div>=A0=A0 =A0 =A0// Write the formatted code to the page.</div><div>=A0=
=A0 =A0 =A0tagProcessorContext.currentBuffer().append(formattedCode);</div>=
<div>=A0=A0 =A0}</div><div>=A0=A0 =A0// Write the &lt;/pre&gt; tag back out=
 to the page.</div><div>=A0=A0 =A0tag.writeTo(tagProcessorContext.currentBu=
ffer());</div>
<div>=A0=A0}</div><div><br></div><div>=A0=A0// Plug in your actual formatti=
ng code here....</div><div><br></div><div>=A0=A0private boolean shouldForma=
tLanguage(String language) {</div><div>=A0=A0 =A0...</div><div>=A0=A0}</div=
><div><br></div><div>
=A0=A0private String formatCode(String language, String code) {</div><div>=
=A0=A0 =A0...</div><div>=A0=A0}</div><div><br></div><div>}</div><div><br></=
div><div>* You create a TagRuleBundle class, which tells SM how to install =
your rule into the parser.</div>
<div><br></div><div><div>public class CodeFormattingTagRuleBundle implement=
s TagRuleBundle {</div><div>=A0</div><div>=A0=A0@Override</div><div>=A0=A0p=
ublic void install(State page, ContentProperty contentProperty, SiteMeshCon=
text context) {</div>
<div>=A0=A0 =A0page.addRule(&quot;pre&quot;, new CodeFormattingTagRule());<=
/div><div>=A0=A0}</div><div>=A0</div><div>=A0=A0@Override</div><div>=A0=A0p=
ublic void cleanUp(State page, ContentProperty contentProperty, SiteMeshCon=
text context) {</div>
<div>=A0=A0 =A0// Nothing needed.</div><div>=A0=A0}</div><div>=A0</div><div=
>}</div><div><br></div></div><div>* Finally, you need to let SM know about =
your TagRuleBundle. See the last section of=A0<a href=3D"http://www.sitemes=
h.org/configuration.html">http://www.sitemesh.org/configuration.html</a></d=
iv>
<div><br></div><div>thanks</div><div>-Joe</div><div><br></div><div class=3D=
"gmail_quote">On Tue, Oct 27, 2009 at 7:24 AM, sheeep <span dir=3D"ltr">&lt=
;<a href=3D"mailto:[email protected]">[email protected]</a>&gt;</=
span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;"><br>
Hello,<br>
<br>
I don&#39;t know if this is the right place to post this, but i&#39;ll give=
 it a<br>
try:)<br>
<br>
I just downloaded sitemesh and looked over some examples (the very few I<br=
>
could find).<br>
I have to edit the pre tags, from a jsp or html page, that contain java<br>
code. (make the comments italic, keywords red), but<br>
I am not quite sure how to approach this with sitemesh. Any help would be<b=
r>
much appreciated :-)<br>
<br>
Thank u so much in advance<br>
<font color=3D"#888888">--<br>
View this message in context: <a href=3D"http://www.nabble.com/Edit-pre-tag=
s-with-sitemesh-tp26076735p26076735.html" target=3D"_blank">http://www.nabb=
le.com/Edit-pre-tags-with-sitemesh-tp26076735p26076735.html</a><br>
Sent from the OpenSymphony - SiteMesh mailing list archive at Nabble.com.<b=
r>
<br>
<br>
---------------------------------------------------------------------<br>
To unsubscribe, e-mail: <a href=3D"mailto:[email protected]=
va.net">[email protected]</a><br>
For additional commands, e-mail: <a href=3D"mailto:[email protected].=
java.net">[email protected]</a><br>
<br>
</font></blockquote></div><br></div>

--001485f5b0c0fd323004774df378--