How to preserve whitespace indentation of text enclosed in HTML <pre> tags excluding the current indentation level of the <pre> tag in the document?

Posted by Michael Barton on Stack Overflow See other posts from Stack Overflow or by Michael Barton
Published on 2011-01-08T00:48:56Z Indexed on 2011/01/08 0:54 UTC
Read the original article Hit count: 309

Filed under:
|
|
|

I'm trying to display my code on a website but I'm having problems preserving the whitespace indentation correctly.

For instance given the following snippet:

<html>
 <body>
   Here is my code:
   <pre>
     def some_funtion
       return 'Hello, World!'
     end
   </pre>
 <body>
</html>

This is displayed in the browser as:

Here is my code:

     def some_funtion
       return 'Hello, World!'
     end

When I would like it displayed as:

Here is my code:

def some_funtion
 return 'Hello, World!'
end

The difference is that that current indentation level of the HTML pre tag is being added to the indentation of the code. I'm using nanoc as a static website generator and I'm using google prettify to also add syntax highlighting.

Can anyone offer any suggestions?

© Stack Overflow or respective owner

Related posts about html

Related posts about whitespace