Skip to content

Instantly share code, notes, and snippets.

@neoneo40
Last active August 29, 2015 14:26
Show Gist options
  • Save neoneo40/4ee671d210902362ffff to your computer and use it in GitHub Desktop.
Save neoneo40/4ee671d210902362ffff to your computer and use it in GitHub Desktop.
How to highlight html file with python
import jsbeautifier
from BeautifulSoup import BeautifulSoup as bs
from IPython.display import HTML
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
lexer = get_lexer_by_name('html')
# <pygments.lexers.HtmlLexer>
formatter = HtmlFormatter(style='default', linenos=False, full=True)
#<pygments.formatters.html.HtmlFormatter at 0x10adf04d0>
response = w.print_problem_source(14)
'''
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>Challenge 14</title>
<style type="text/css">
body { background:black; color:white; font-size:10pt; }
</style>
</head>
<body>
<br /><br />
<form name="pw"><input type="text" name="input_pwd" /><input type="button" onclick="ck()" value="check" /></form>
<script>
function ck()
{
var ul=document.URL;
ul=ul.indexOf(".kr");
ul=ul*30;
if(ul==pw.input_pwd.value) { alert("Password is "+ul*pw.input_pwd.value); }
else { alert("Wrong"); }
}
</script>
</body></html>
'''
# 1안: indent 정리되지 않고 그냥 colorful 하게만 나옴
data = highlight(response, lexer, formatter)
# 2안: jsbeautifier 사용
HTML(data=highlight(jsbeautifier.beautify(response), lexer, formatter))
'''
< html xmlns = "http://www.w3.org/1999/xhtml" > < head >
< title > Challenge 14 < /title> < style type = "text/css" >
body {
background: black;color: white;font - size: 10 pt;
} < /style> < /head> < body >
< br / > < br / >
< form name = "pw" > < input type = "text"
name = "input_pwd" / > < input type = "button"
onclick = "ck()"
value = "check" / > < /form> < script >
function ck() {
var ul = document.URL;
ul = ul.indexOf(".kr");
ul = ul * 30;
if (ul == pw.input_pwd.value) {
alert("Password is " + ul * pw.input_pwd.value);
} else {
alert("Wrong");
}
}
< /script>
< /body></html >
'''
# 3안: BeautifulSoup 사용
soup = bs(response)
'''
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Challenge 14
</title>
<style type="text/css">
body { background:black; color:white; font-size:10pt; }
</style>
</head>
<body>
<br />
<br />
<form name="pw">
<input type="text" name="input_pwd" />
<input type="button" onclick="ck()" value="check" />
</form>
<script>
function ck()
{
var ul=document.URL;
ul=ul.indexOf(".kr");
ul=ul*30;
if(ul==pw.input_pwd.value) { alert("Password is "+ul*pw.input_pwd.value); }
else { alert("Wrong"); }
}
</script>
</body>
</html>
'''
# 1안은 highlight만
# 2안은 indent 되고 highlight 되는데 </script>로 나와야 되는데 < /script>로 나옴... 왜 space가 들어가는건지..?
# space만 어떻게 설정값으로 뺄 수 있다면 이게 가장 좋은 선택인듯.. 정 안되면 regex로..??!!
# 3안은 잘 되는데 script 안이 indent가 제대로 안됨...
# if(ul==pw.input_pwd.value) { alert("Password is "+ul*pw.input_pwd.value); }
# else { alert("Wrong"); }
# }
# 이렇게 나와서 너무 읽기가 불편
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment