I am working on writing a simple blog in Golang using Martini, the Martini-Contrib Renderer package, and Blackfriday.
I am able to get the post into the DB and out of the DB with no issues. I even get the Body of the post out of the DB and into my struct as html however when we render the template the output is just plain text html and not looking pretty like it should.
Code is hosted here:
http://bitbucket.org/ChasingLogic/goblog
Any help would be great.
EDIT:
You can see what it's doing here:
http://chasinglogic.com/
Golang templates escape variables by default. You can use template.HTML instead of string when it contains HTML and the source is trusted (which, in this instance, it seems to be).
http://golang.org/pkg/html/template/#HTML
type HTML string
HTML encapsulates a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. The outputs of a sound HTML sanitizer and a template escaped by this package are fine for use with HTML.
The way I would fix it would be by changing this
type Post struct {
Title string
Body string
Author string
Date string
}
to
type Post struct {
Title string
Body template.HTML
Author string
Date string
}
And then change
post.Body = string(blackfriday.MarkdownCommon([]byte(preFormatMarkdown)))
to
post.Body = template.HTML(blackfriday.MarkdownCommon([]byte(preFormatMarkdown)))
Related
I'm passing down a variable to a django template that contains an html. For example <strong>example</strong>. I mark this string as mark_safe() before storing it in my variable.
When I load it into the template and load the page in my browser it shows the html as plain text, <strong>example</strong>.
If I look at it in the chrome console the only thing that is different is that the text is surround with parenthesis. So it would look like this, "<strong>example</strong>"
Like I said I've read through all the other stackoverflow posts and marked the variables using the {% autoescape off %} tags and I've tried 'safe' tag. These will remove the escaping, but the HTML still doesn't render. Below is the actual html unescaped. I'm wondering if it's the space in front of it?
<p>Modern Comics That Are Valuable But Often Overlooked and Should Be Sought Out In Dollar Bins and In Your Own Collection</p>
<p><strong><em>Its Like Having $-Ray Vision</em></strong></p>
Thanks for the help.
The escaped string first needs to be parsed to HTML. Then you can unescape that string and pass it down and it will be rendered correctly.
import html.parser
html_parser = html.parser.HTMLParser()
description = html_parser.unescape(category.description)
I would like my webpage to display what resembles JASON Parser
does (right panel). I first installed JSONView on my Chrome.
Then I composed a string {"isbn":"asdf","name":"qwer","price":1} which I fed to JSON Parser and made sure it indeed obeyed JSON format.
Eventually, I attempted to display it on my webpage by doing:
out.println("{\"isbn\":\"asdf\",\"name\":\"qwer\",\"price\":1}\");
but it was simply displayed like any other normal String instead of being formatted like this.
How do I manage to make my webpage automatically display JSON formatted?
Thanks!
Edit: I didn't realise this was JSP. The answer I'm giving relates to using the JavaScript method JSON.stringify() which you'd somehow achieve within client-side JavaScript within your JSP project.
Use JSON.stringify() to 'pretty-print' the original JavaScript object.
For example, to have two spaces of indentation:
var str = JSON.stringify(obj, null, 2);
If this has to be achieved within Java code then you could use the GSON library. Example: Pretty-Print JSON in Java
I've a JSON containing some HTML contents from an External System. We have a Rich Text field for storing this HTML data. But I noticed, while storing I'm getting some HTML tags included in to the field as the HTML contents are coming as JSON string. So my question is how can I store the received JSON string data as a HTML back in Netsuite field. Is it possible ?
Jdata = dataIn.desc; // getting something like : Guest(s) benifit's surcharges <br><p>test benifit desc 25% discountpop "test"</p>
Thanks for your interest !
Maybe try unescape or decodeuri, which are standard old js methods.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape
If not just do a string replacement on the known char sets (" being the same as ")
I'm using Play framework to render my UI for the browser. I have a String field that I want to display in the browser and the value for this String comes from the database. This String field however contains text with HTML formatting as below:
This exam contains questions about core Java initialization, declaration, contro, structures. Questions will be displayed as below:<br><br>public static void main(String args[ ]){<br> int x = 10;<br>}
When I render this in the browser, it does not show properly formatted. Here is what the HTML rendering looks like!
Here is how the HTML looks like in the browser!
Any ideas?
From the documentation on Scala templates:
Escaping
By default, dynamic content parts are escaped according to
the template type’s (e.g. HTML or XML) rules. If you want to output a
raw content fragment, wrap it in the template content type.
For example to output raw HTML:
<p>
#Html(article.content)
</p>
I am using LINQ-to-XML. I am building a small program that helps parse HTML. I'd like to save the HTML tags into an XML file, but I don't want the XML file to check the validity of the entered HTML elements.
How can I just entere a simple string literal (a pretty long one)?
Maybe using a CDATA construct could help you out, see w3schools.com