-2

I'm writing a C++ Style Guide for my company in html/css/javascript. I'm quite irritated with html as it treats anything between < and > as html tag and thus processes them as well. As a result of which my code (which I put in the style guide) doesn't look as such. Here is an example:

<pre>
std::vector<std::string>  get_project_names();    

template<typename Printable>
void print(Printable const & item);    

template<typename FwdIterable, typename Predicate>
FwdIterable find_if(FwdIterable begin, FwdIterable end, Predicate pred);
</pre>

and I want the browser to render it exactly like that, but it doesn't render so, e.g Chrome doesn't show <std::string> part, and IE 8.0 capitalize <std::string> as <STD::STRING> (and all such template codes).

I don't want any kind of interference by html engine. Is there any simple way to achieve what I want? Any polite way to tell the browser to not modify my code?

Note that replacing < with &lt; and > with &gt; would work, but it is cumbersome to write it everytime I write a template code. It also makes my code difficult to read in the source code of the html. So I'm looking for a simple solution.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Are you simply looking for `<` and `>` …? – CBroe Jun 28 '14 at 17:46
  • 1
    @Anonymous: That supposed duplicate doesn't answer my question, as it says **"But this only works in XML (and hence XHTML, but not all browsers support it), not in HTML."** – Nawaz Jun 28 '14 at 17:48
  • 1
    @Nawaz Look below that on the same answer. – Anonymous Jun 28 '14 at 17:48
  • There do seem to be a number of good suggestions on the suggested duplicate. I take it you are writing the HTML by hand, rather than using a preprocessor to transform for your HTML. Is that correct? – Ray Toal Jun 28 '14 at 17:50
  • @Anonymous: Replacing `<` with `<` and `>` with `>` would work, but it is cumbersome to write it everytime I write a template code. I'm looking for simple solution. – Nawaz Jun 28 '14 at 17:50
  • 1
    @Nawaz You can use some tool to speed up the process if you want, but that's the answer. Try looking at Stack Overflow's source for example. – Anonymous Jun 28 '14 at 17:53
  • Write a simple C++ program that converts `<`, `>`, `&` to `<`, `>`, `&` respectively, and do some copy and paste. – Nisse Engström Jun 28 '14 at 18:15

4 Answers4

1

The notion of a "polite way to to tell the browser to not modify (parse) my code" is precisely what XML's CDATA does. Nothing more, nothing less.

CDATA does not exist in HTML, so there is no way in HTML to treat <std:vector> as anything other than on opening tag for the (non-existent) std:vector element.

The normal way to do this is a server-side transformation. Now if you aren't generating your HTML server-side, and are instead writing it by hand, you can make your life just a dash easier with a client-side transformation like this:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Page Title</title>
  <script src="http://coffeescript.org/extras/coffee-script.js"></script>
</head>
<body>
  <pre><script type="text/coffeescript" >document.write "

    std::vector<std::string>  get_project_names();  

  ".replace('<','&lt;')
  </script></pre>
</body>
</html>

Here I used CoffeeScript because of its multiline string capability which is coming in ES6 for regular JavaScript. It makes it easy to just drop in your code between the boilerplate lines.

Now I know full well even this solution is lacking! If your inserted code contains a " you're out of luck. And it doesn't escape ampersands.

Bottom line is that there is no CDATA, so no "simple" solution exists. A transformation, client-side or server-side, is required.

Have you tried markdown?

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

I've been dealing with this particular problem for years, and it's always been frustrating. I've always appreciated the simplicity and elegance of Markdown, so I did a little research to see if there was any way to use Markdown to build an HTML document.

Thing is, code samples sometimes involve HTML, yet HTML is the language we're using to write style guides and API documentation, so my thought was that if we wrote the API documentation and style guides in Markdown, we'd eliminate all of the conflicts between HTML and the syntax of other languages.

I found Strapdown.js, which is a library that allows you to create a Web page with pure Markdown. The library then compiles it to HTML and renders it on the page client side. We put together the API documentation for one of our products using this library, and we published it as a GitHub page.

Here's a small, concise example:

<!DOCTYPE html>
<html>
<title>JavaScript API</title>

<xmp theme="united" style="display:none;">

## Print the name

Print the user's name:

```javascript
function printName(name) {
    alert(name);
}
```
</xmp>

<script src="http://strapdownjs.com/v/0.2/strapdown.js"></script>
</html>

Everything inside the <xmp> tags gets compiled to HTML.


Note: The XMP tag has been deprecated for some time as per the Mozilla HTML documentation on XMP. Thus, you may want to either hack the code to make it use PRE or CODE, or you may want to consider using the lower-level Marked library that was used to build Strapdown.js. I filed an issue with the Strapdown.js team.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
0

For that you can use this

<pre>
  std::vector&lt;std::string&gt;  get_project_names();    

  template&lt;typename Printable&gt;
  void print(Printable const & item);    

  template&lt;typename FwdIterable, typename Predicate&gt;
  FwdIterable find_if(FwdIterable begin, FwdIterable end, Predicate pred);
</pre>

This would be encoded and you'll get the result that you want.

Here is the fiddle for that: http://jsfiddle.net/afzaal_ahmad_zeeshan/7B9xB/

JavaScript code

The JavaScript method of doing this would be simple, you can convert the whole code to a String variable.

As this

var string = "content here";

Then apply this,

string.replace('<','&lt;').replace('>','&gt;');

Convert all the characters and then have then rendered by the Browser.

http://jsfiddle.net/afzaal_ahmad_zeeshan/7B9xB/1/

Community
  • 1
  • 1
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • It sounds like the asker is writing code by hand. Wrapping it up in a string every time can be just as cumbersome as converting the `>` and `<` to `<` and `>` every time. See http://stackoverflow.com/questions/24469418/how-to-preserve-c-template-code-in-html/24469696#comment37870969_24469418 – jamesmortensen Jun 28 '14 at 18:47
0

For my book I used http://markup.su/highlighter/ syntax highlighter. Paste the code into it, generate highlighted code, and paste the latter into the HTML document. Worked pretty well. Here's a fiddle with your code: http://jsfiddle.net/6GTs2/.

Here's your code highlighted for HTML:

<pre style="background:#000;color:#f8f8f8">std::vector&lt;std::string>  <span style="color:#89bdff">get_project_names</span>();    

<span style="color:#99cf50">template</span>&lt;<span style="color:#99cf50">typename</span> Printable> 
<span style="color:#99cf50">void</span> <span style="color:#89bdff">print</span>(Printable const &amp; item);    

<span style="color:#99cf50">template</span>&lt;<span style="color:#99cf50">typename</span> FwdIterable, <span style="color:#99cf50">typename</span> Predicate> 
FwdIterable <span style="color:#89bdff">find_if</span>(FwdIterable begin, FwdIterable end, Predicate pred);
</pre>
DRD
  • 5,557
  • 14
  • 14