0

I'm trying to build an HTML element library (like Twitter Bootstrap) and I'd like to have a live version of the element (like a button, for example) next to the proper code markup.

I'm trying to use Prettify to display something like <button>Button</button>, but it doesn't seem to be working.

Here's what I'm seeing when I view the page:

enter image description here

Question: What am I doing wrong here?

Here's the relevant HTML:

<html>
<head>
<title>Common HTML Library</title>

<link type="text/css" rel="stylesheet" href="common.css" />
<link type="text/css" rel="stylesheet" href="prettify/prettify.css" />

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="prettify/prettify.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        prettyPrint();
    });
</script>
</head>

<body>
<h1>Buttons</h1>
<table>
    <thead>
        <tr>
            <th>Element</th>
            <th>Description</th>
            <th>Markup</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <button>Button</button>
            </td>
            <td>Standard Button</td>
            <td>
                <code class="prettyprint">
                    <button>Button</button>
                </code>
            </td>
        </tr>
        <tr>
            <td>
                <button class="primary">Button</button>
            </td>
            <td>Primary Button</td>
            <td>
                <code class="prettyprint">
                    <button class="primary">Button</button>
                </code>
            </td>
        </tr>
        <tr>
            <td>
                <button disabled="disabled">Button</button>
            </td>
            <td>Disabled Button</td>
            <td>
                <code class="prettyprint">
                    <button disabled="disabled">Button</button>
                </code>
            </td>
        </tr>
    </tbody>
</table>
</body>
</html>

EDIT:

I tried setting the language and using <pre> like this and got the following:

<pre class="prettyprint lang-html">
    <button>Button</button>
</pre>

enter image description here

Jon
  • 3,154
  • 13
  • 53
  • 96

1 Answers1

1

Try setting the language and escaping the brackets/quotes:

<code class="prettyprint lang-html">
    &lt;button class=&quot;primary&quot;&gt;Button&lt;/button&gt;
</code>

Update:

Hmm, actually I think you need to use <pre> instead of <code>

Update 2: Try this code (demo):

$(document).ready(function() {
  $('.prettyprint').html(function(i,h){
    return h.replace(/[<>\"\'\t\n]/g, function(m) { return {
      '<' : '&lt;',
      '>' : '&gt;',
      "'" : '&#39;',
      '"' : '&quot;',
      '\t': '  ',
      '\n': '<br/>' // needed for IE
    }[m]});
  });

  prettyPrint();
});
Mottie
  • 84,355
  • 30
  • 126
  • 241