开发者

Display files on HTML page as it is

I am using webpy framework for my project. I want to pass a file from my webpy program and display it on html page as it is(files may be any text files/program files). I passed a text file using following function from my webpy program.

class display_files:
    def GET(self):
        wp=web.input()
        file_name=wp.name
        repo_name=wp.repo
        repo_path=os.path.join('repos',repo_name)
        file_path=os.path.join(repo_path,file_name)
        fp=open(file_path,'rU')             #reading file from file path
        text=fp.read()                      #no problem found till this line.
        fp.close()            
        return render.file_display(text) #calling file_display.html 

When I tried to display the file (here it is 'text') from 'file_display.html', it displays continuously without recognising newline.Here is my html file.

$def with(content)
<html>
<head>
    <meta http-equiv="Content-Type" content="text/string;charset=utf-8" >
    <title>File content</title>
</head>
<body>
    <form name="file_content" id="file_content" methode="GET">
        <p> $content开发者_运维百科</p>
    </form>
</body>
<html>

How can I display file as it is in html page.


HTML treats amount of whitespace characters as a single whitespace. If the file that you are displaying contains this text:

line one
  line two with indent

the rendered file_display.html will contain this HTML:

<p> line one
  line two with indent</p>

Still, the newline and two spaces will be treated as a single space, and in browser it will look like this:

line one line two with indent

The pre element tells the browser that the text inside it is preformatted, so newlines and spaces should be kept. Thus, your template should look like:

<form name="file_content" id="file_content" methode="GET">
    <pre>$content</pre>
</form>

As for Joseph's advice, web.py templating system will handle escaping for you. If your file contains characters like < or >, they will be replaced with &lt; and &gt;.


looks like you may need to globally replace any < or > with &lt; or &gt; respectively:

http://jsfiddle.net/BaULp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜