title: Render a c program into a html using python
body: <p><strong>Task is to render a c program into an HTML file using python</strong></p>
  <p>this is my code</p>
  <pre><code>from jinja2 import Template
  
  html_template = '''
      &lt;html&gt;
          &lt;body&gt;
              &lt;div&gt;
              {% for i in code %}  {{i}}&lt;br&gt; {% endfor %}
              &lt;/div&gt;
          &lt;/body&gt; 
      &lt;/html&gt;'''
  
  # reading c program file
  file_content = open(&quot;prog.c&quot;,'r').readlines()
  template = Template(html_template)
  html_content = template.render(code = file_content)
  
  with open('outp.html','w') as f:
      f.write(html_content)
  </code></pre>
  <p>consider the prog.c file contains</p>
  <pre><code>#include &lt;stdio.h&gt;
  int main(void) {
      printf(&quot;Hello World!&quot;);
      }
  </code></pre>
  <p>The HTML output should be</p>
  <pre>
  
      #include &lt;stdio.h>
      int main(void) {
          printf("Hello World!");
          }
  
  </pre>
  <p>but what I get is</p>
  <pre>
  
      #include
      int main(void) {
      printf("Hello World!");
      }
  
  </pre>
  <p>I have two problems in doing this:</p>
  <ol>
  <li>The indentation before 'printf...' line should also reflect in HTML.</li>
  <li>The <code>&lt;stdio.h&gt;</code> in c code is considered as HTML tag while rendering, I need it as text in html.</li>
  </ol>
  <p>Note: Constraints</p>
  <ol>
  <li>should not change the prog.c file since it will always be dynamic.</li>
  </ol>
  <p>you can use python string formatting or jinja templating that is up to you, I just need a solution.</p>
  <p>Can you please suggest me some idea?</p>