I'm looking at outputting Rich Text in Magnolia directly to the front-end. I'm defining the field as below:
#TabFactory("Content")
public void contentTab(UiConfig cfg, TabBuilder tab) {
tab.fields(
cfg.fields.text("title").label("Title"),
cfg.fields.richText("subtitle").label("Subtitle")
);
}
Within a template, when information is saved into the JCR it appears to encode the data with HTML entities:
Title: ${content.title}
Subtitle: ${content.subtitle}
Outputs (raw source) ...
Title: The Title Field
Subtitle: <p>The Subtitle Field</p>
But should output (raw source) ...
Title: The Title Field
Subtitle: <p>The Subtitle Field</p>
Is there a way to stop the Rich Text fields from being encoded automatically?
The decode function works:
https://documentation.magnolia-cms.com/display/DOCS/cmsfn#cmsfn-DecodeHTML
[#if content.text?has_content]
${cmsfn.decode(content).text}
[/#if]
Most framework try to get rid of the XSS-kind of attacks so it is a good way to use the template as the following:
Title: ${content.title}
Subtitle: <p>${content.subtitle}</p>
It will prevent the users (or admins) to run magic JS or some other bad codes on the client.
Back to the question: check out the documentation here https://documentation.magnolia-cms.com/display/DOCS/Component+definition. There is something called escapeHtml take a look at it. :)
Edit: as bashaus pointed out he was using page properties not components. The solution in this way the following:
[#if content.text?has_content]
${cmsfn.decode(content).text}
[/#if]
TL.DR: Try to avoid html characters in dynamic content, but the feature is able to be turned off.
Related
I have a REST service method annotated with
io.swagger.annotations.ApiOperation
#ApiOperation(value = "some string")
I need some string to generate newlines in the HTML page swagger-ui.html
(i.e. in the Swagger UI page where I see my docs and I can test my API methods).
I tried putting <br/> and \n in some string, and anything I could think of,
and anything I could find as suggestions here on SO... but nothing works.
Any ideas? Or is it just not possible?
I see the project uses Swagger 1.5.20 JARs.
That version I cannot change.
swagger-annotations-1.5.20.jar
swagger-models-1.5.20.jar
I am afraid that you cannot put line breaks into the summary field as opposed to description field. Check the comment from a member of Swagger team: Swagger UI ignores line breaks in description
The summary field is displayed as a single line because the summary text is inside of a <span> element. Being an inline element, the span ignores \n characters.
Further, your <br> is not showing up because the summary field is a simple string field, as opposed to description, which is a Markdown field ...
Can I use jsoup to parse non-standard markup, such as <LOCATION>, <PERSON>, or <ORGANIZATION>?
This is an example sentence in my corpus:
I HAD been hearing about vineyards in <LOCATION>Malibu</LOCATION> for some time,
but I wrote them off. Had to be a tourist gimmick, like
<PERSON>Knott</PERSON>'s <ORGANIZATION>Berry Farm</ORGANIZATION>
or the LaBrea Tar Pits. <LOCATION>Malibu</LOCATION> was the playground of the stars,
a surfers' mecca, but cabernet? No way.
I'd like to extract something like:
Location: Malibu
Person: Knott
Organization: Berry Farm
If it is not part of the HTML specification the default parse method will not handle the custom markup.
You can however tell JSoup to parse it as an XML:
Jsoup.parse(yourHtml, baseUriForLinks, Parser.xmlParser());
The command above will return a Document in which you can operate with your custom markup.
Where:
yourHtml - the HTML with the custom markup as String
baseUriForLinks - the base URL of the HTML (so that JSoup can resolve relative links if any are present) also as String
I'm trying to figure out a way to parse a html file with custom tags in the form:
[custom tag="id"]
Here's an example of a file I'm working with:
<p>This is an <em>amazing</em> example. </p>
<p>Such amazement, <span>many wow.</span> </p>
<p>Oh look, a wild [custom tag="amaze"] appears.</p>
We need maor embeds <a href="http://youtu.be/F5nLu232KRo"> bro
What I would like (in an ideal world) is to get back is a list of elements):
List foundElements = [text, custom tag, text, link, text]
Where the element in the above list contains:
Text:
<p>This is an <em>amazing</em> example. </p>
<p>Such amazement, <span>many wow.</span> </p>
<p>Oh look, a wild [custom tag="amaze"] appears.</p>
We need maor embeds
Custom tag:
[custom tag="amaze"]
Link:
<a href="http://youtu.be/F5nLu232KRo">
Text:
appears.</p>We need maor embeds
What I've tried:
Jsoup
Jsoup is great, it works perfectly for HTML. The issue is I can't define custom tags with opening "[" and closing "]". Correct me if I'm wrong?
Jericho
Again like Jsoup, Jericho works great..except for defining custom tags. You're required to use "<".
Java Regex
This is the option I really don't want to go for. It's not reliable and there's a lot of string manipulation that is brittle, especially when you're matching against a lot of regexes.
Last but not least, I'm looking for a performance orientated solution as this is done on an Android client.
All suggestions welcome!
Is there an easy way to send HTML from a servlet to a JSP, using AJAX.
I've already figured out how to make AJAX work with servlets dynamically, but now I want to press a button on a form and generate HTML based on text-input.
Is it possible, and if so, how, to send just pieces of HTML to an existing HTML page?
Example,
I have a basic form where you can input your age, and based on the age the text has a different size/color. So, you send for example, 25 as your age to the servlet, and it send back a piece of HTML like this <p STYLE="font-size: age;"> to the page.
Through ajax call you can get the output result either a string, html or a Json object that will be parsed and results can be displayed over JSP/HTML. So for sure you can send html code segment from servlet to jsp through ajax call.
For example you can use this approach--
1. Take a string variable in your servlet.
2. Put appropriate html string as per your conditions in this string variable
3. send this string as a response from servlet like:
response.setCharacterEncoding("UTF-8");
response.getWriter().write("your string variable here");
4. In your ajax call do like this:
success : function(dataString) {
document.getElementById("containerId").innerHTML=dataString;
},
where containerId is the id of html element (like div or span) where you want to display output html.
The easiest approach, without client-side javascript libraries, would be to point an HTML form to an iframe, just like
<iframe name="myIframe"...>
<form target="myIframe"...>
And submit your form as many times as necessary. The HTML returned by the servlet would load itself in the iframe element.
If you like AJAX and client-side javascript libraries, you can find very easy programmatical ways to do this in jQuery and similar libraries.
Basically your servlet can generate any kind of content, e.g. JSON, HTML etc.
You'd then send that content back to the client and integrate it into the page.
How that is done depends on the type of content.
Example:
You issue an AJX request (e.g. by using jQuery's ajax functionality) and your servlet generates plain html. When your JavaScript receives the anser you just replace the relevant part, e.g. by replacing the content of some defined element.
If you used JSON instead, your servlet might send data only instead, e.g. a font size based on the age as in your example. You'd then use JavaScript to access that JSON data and perform relevant operations, e.g. by changing the style of the paragraph.
I'm trying to pass a simple URL to a view within a play framework app, however when passed as a string, the & in the url is changed to & which causes the URL to not work.
If I change the argument to Html, i.e #(url: Srting) to #(url: Html), then I get an error when I try to pass the url as string to view.render() method.
How can I convert the url into Html and pass it as that?
To prevent the default escaping that happens for dynamic content on views you need to wrap the String with #Html(String) function:
View:
#(url: String)
<div class="myLink">
Go to: #Html(url) <br>
not to: #url
</div>
Controller:
public static Result displayLink(){
return ok(view.render("<a href='http://stackoverflow.com/'>Stack Overflow</a>"));
}
See The template engine page on the documentation for more info (specifically the "Escaping" section at the very bottom).
If you want to render HTML content instead of displaying it as raw text, return the content .as("text/html"). Example:
WS.url("http://www.stackoverflow.com").get().map(resp => Ok(resp.body).as("text/html"))