Difference between revisions of "Team:EPF Lausanne/Project/Modelling/Code/code2python"
(Created page with "from pygments import highlight from pygments.lexers import PythonLexer from pygments.lexers import CppLexer from pygments.formatters import HtmlFormatter # C++ extensions cpp = ...") |
|||
Line 1: | Line 1: | ||
− | + | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
− | + | "http://www.w3.org/TR/html4/strict.dtd"> | |
− | + | ||
− | + | ||
− | + | <html> | |
− | + | <head> | |
+ | <title></title> | ||
+ | <meta http-equiv="content-type" content="text/html; charset=None"> | ||
+ | <style type="text/css"> | ||
+ | td.linenos { background-color: #f0f0f0; padding-right: 10px; } | ||
+ | span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } | ||
+ | pre { line-height: 125%; } | ||
+ | body .hll { background-color: #49483e } | ||
+ | body { background: #272822; color: #f8f8f2 } | ||
+ | body .c { color: #75715e } /* Comment */ | ||
+ | body .err { color: #960050; background-color: #1e0010 } /* Error */ | ||
+ | body .k { color: #66d9ef } /* Keyword */ | ||
+ | body .l { color: #ae81ff } /* Literal */ | ||
+ | body .n { color: #f8f8f2 } /* Name */ | ||
+ | body .o { color: #f92672 } /* Operator */ | ||
+ | body .p { color: #f8f8f2 } /* Punctuation */ | ||
+ | body .cm { color: #75715e } /* Comment.Multiline */ | ||
+ | body .cp { color: #75715e } /* Comment.Preproc */ | ||
+ | body .c1 { color: #75715e } /* Comment.Single */ | ||
+ | body .cs { color: #75715e } /* Comment.Special */ | ||
+ | body .gd { color: #f92672 } /* Generic.Deleted */ | ||
+ | body .ge { font-style: italic } /* Generic.Emph */ | ||
+ | body .gi { color: #a6e22e } /* Generic.Inserted */ | ||
+ | body .gs { font-weight: bold } /* Generic.Strong */ | ||
+ | body .gu { color: #75715e } /* Generic.Subheading */ | ||
+ | body .kc { color: #66d9ef } /* Keyword.Constant */ | ||
+ | body .kd { color: #66d9ef } /* Keyword.Declaration */ | ||
+ | body .kn { color: #f92672 } /* Keyword.Namespace */ | ||
+ | body .kp { color: #66d9ef } /* Keyword.Pseudo */ | ||
+ | body .kr { color: #66d9ef } /* Keyword.Reserved */ | ||
+ | body .kt { color: #66d9ef } /* Keyword.Type */ | ||
+ | body .ld { color: #e6db74 } /* Literal.Date */ | ||
+ | body .m { color: #ae81ff } /* Literal.Number */ | ||
+ | body .s { color: #e6db74 } /* Literal.String */ | ||
+ | body .na { color: #a6e22e } /* Name.Attribute */ | ||
+ | body .nb { color: #f8f8f2 } /* Name.Builtin */ | ||
+ | body .nc { color: #a6e22e } /* Name.Class */ | ||
+ | body .no { color: #66d9ef } /* Name.Constant */ | ||
+ | body .nd { color: #a6e22e } /* Name.Decorator */ | ||
+ | body .ni { color: #f8f8f2 } /* Name.Entity */ | ||
+ | body .ne { color: #a6e22e } /* Name.Exception */ | ||
+ | body .nf { color: #a6e22e } /* Name.Function */ | ||
+ | body .nl { color: #f8f8f2 } /* Name.Label */ | ||
+ | body .nn { color: #f8f8f2 } /* Name.Namespace */ | ||
+ | body .nx { color: #a6e22e } /* Name.Other */ | ||
+ | body .py { color: #f8f8f2 } /* Name.Property */ | ||
+ | body .nt { color: #f92672 } /* Name.Tag */ | ||
+ | body .nv { color: #f8f8f2 } /* Name.Variable */ | ||
+ | body .ow { color: #f92672 } /* Operator.Word */ | ||
+ | body .w { color: #f8f8f2 } /* Text.Whitespace */ | ||
+ | body .mb { color: #ae81ff } /* Literal.Number.Bin */ | ||
+ | body .mf { color: #ae81ff } /* Literal.Number.Float */ | ||
+ | body .mh { color: #ae81ff } /* Literal.Number.Hex */ | ||
+ | body .mi { color: #ae81ff } /* Literal.Number.Integer */ | ||
+ | body .mo { color: #ae81ff } /* Literal.Number.Oct */ | ||
+ | body .sb { color: #e6db74 } /* Literal.String.Backtick */ | ||
+ | body .sc { color: #e6db74 } /* Literal.String.Char */ | ||
+ | body .sd { color: #e6db74 } /* Literal.String.Doc */ | ||
+ | body .s2 { color: #e6db74 } /* Literal.String.Double */ | ||
+ | body .se { color: #ae81ff } /* Literal.String.Escape */ | ||
+ | body .sh { color: #e6db74 } /* Literal.String.Heredoc */ | ||
+ | body .si { color: #e6db74 } /* Literal.String.Interpol */ | ||
+ | body .sx { color: #e6db74 } /* Literal.String.Other */ | ||
+ | body .sr { color: #e6db74 } /* Literal.String.Regex */ | ||
+ | body .s1 { color: #e6db74 } /* Literal.String.Single */ | ||
+ | body .ss { color: #e6db74 } /* Literal.String.Symbol */ | ||
+ | body .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ | ||
+ | body .vc { color: #f8f8f2 } /* Name.Variable.Class */ | ||
+ | body .vg { color: #f8f8f2 } /* Name.Variable.Global */ | ||
+ | body .vi { color: #f8f8f2 } /* Name.Variable.Instance */ | ||
+ | body .il { color: #ae81ff } /* Literal.Number.Integer.Long */ | ||
− | + | </style> | |
− | + | </head> | |
+ | <body> | ||
+ | <h2></h2> | ||
− | def load_file_as_sting(fname): | + | <div class="highlight"><pre><span class="kn">from</span> <span class="nn">pygments</span> <span class="kn">import</span> <span class="n">highlight</span> |
− | "" | + | <span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="n">PythonLexer</span> |
− | + | <span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="n">CppLexer</span> | |
− | + | <span class="kn">from</span> <span class="nn">pygments.formatters</span> <span class="kn">import</span> <span class="n">HtmlFormatter</span> | |
+ | |||
+ | <span class="c"># C++ extensions</span> | ||
+ | <span class="n">cpp</span> <span class="o">=</span> <span class="p">[</span><span class="s">"cpp"</span><span class="p">,</span><span class="s">"cxx"</span><span class="p">,</span><span class="s">"cc"</span><span class="p">,</span><span class="s">"h"</span><span class="p">]</span> | ||
+ | |||
+ | <span class="c"># Python extensions</span> | ||
+ | <span class="n">py</span> <span class="o">=</span> <span class="p">[</span><span class="s">"py"</span><span class="p">]</span> | ||
+ | |||
+ | <span class="k">def</span> <span class="nf">load_file_as_sting</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span> | ||
+ | <span class="sd">"""</span> | ||
+ | <span class="sd"> Open the file FNAME and save all its content in an unformatted string</span> | ||
+ | <span class="sd"> """</span> | ||
− | content = "" | + | <span class="n">content</span> <span class="o">=</span> <span class="s">""</span> |
− | with open(fname, | + | <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span><span class="s">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="c"># Open the file (read only)</span> |
− | content = f.read() # Read file and store it in an unformatted string | + | <span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="c"># Read file and store it in an unformatted string</span> |
− | # The file is automatically closed | + | <span class="c"># The file is automatically closed</span> |
− | return content | + | <span class="k">return</span> <span class="n">content</span> |
− | def save_string_as_file(fname,string): | + | <span class="k">def</span> <span class="nf">save_string_as_file</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span><span class="n">string</span><span class="p">):</span> |
− | "" | + | <span class="sd">"""</span> |
− | + | <span class="sd"> Save the unformatted string STRING into the file FNAME</span> | |
− | + | <span class="sd"> """</span> | |
− | with open(fname, | + | <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span><span class="s">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="c"># Open the file (write only)</span> |
− | f.write(string) | + | <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">string</span><span class="p">)</span> |
− | # The file is automatically closed | + | <span class="c"># The file is automatically closed</span> |
− | def lexer_formatter(language,css=False): | + | <span class="k">def</span> <span class="nf">lexer_formatter</span><span class="p">(</span><span class="n">language</span><span class="p">,</span><span class="n">css</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span> |
− | "" | + | <span class="sd">"""</span> |
− | + | <span class="sd"> Return the lexer for the appropriate language and the HTML formatter</span> | |
− | + | <span class="sd"> """</span> | |
− | L = None | + | <span class="n">L</span> <span class="o">=</span> <span class="bp">None</span> |
− | if language in py: | + | <span class="k">if</span> <span class="n">language</span> <span class="ow">in</span> <span class="n">py</span><span class="p">:</span> |
− | # Python Lexer | + | <span class="c"># Python Lexer</span> |
− | L = PythonLexer() | + | <span class="n">L</span> <span class="o">=</span> <span class="n">PythonLexer</span><span class="p">()</span> |
− | elif language in cpp: | + | <span class="k">elif</span> <span class="n">language</span> <span class="ow">in</span> <span class="n">cpp</span><span class="p">:</span> |
− | # C++ Lexer | + | <span class="c"># C++ Lexer</span> |
− | L = CppLexer() | + | <span class="n">L</span> <span class="o">=</span> <span class="n">CppLexer</span><span class="p">()</span> |
− | else: | + | <span class="k">else</span><span class="p">:</span> |
− | raise NameError("Invalid language.") | + | <span class="k">raise</span> <span class="ne">NameError</span><span class="p">(</span><span class="s">"Invalid language."</span><span class="p">)</span> |
− | HF = HtmlFormatter(full=css,style="monokai") | + | <span class="n">HF</span> <span class="o">=</span> <span class="n">HtmlFormatter</span><span class="p">(</span><span class="n">full</span><span class="o">=</span><span class="ow">not</span> <span class="n">css</span><span class="p">,</span><span class="n">style</span><span class="o">=</span><span class="s">"monokai"</span><span class="p">)</span> |
− | return L, HF | + | <span class="k">return</span> <span class="n">L</span><span class="p">,</span> <span class="n">HF</span> |
− | def code_to_htmlcss(code,language): | + | <span class="k">def</span> <span class="nf">code_to_htmlcss</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">language</span><span class="p">):</span> |
− | "" | + | <span class="sd">"""</span> |
− | + | <span class="sd"> Transform CODE into html and css (separate files)</span> | |
− | + | <span class="sd"> """</span> | |
− | # Obtain lexer and HtmlFormatter | + | <span class="c"># Obtain lexer and HtmlFormatter</span> |
− | L, HF = lexer_formatter(language,css=True) | + | <span class="n">L</span><span class="p">,</span> <span class="n">HF</span> <span class="o">=</span> <span class="n">lexer_formatter</span><span class="p">(</span><span class="n">language</span><span class="p">,</span><span class="n">css</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> |
− | # Create html code | + | <span class="c"># Create html code</span> |
− | html = highlight(code,L,HF) | + | <span class="n">html</span> <span class="o">=</span> <span class="n">highlight</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">L</span><span class="p">,</span><span class="n">HF</span><span class="p">)</span> |
− | # Create css code | + | <span class="c"># Create css code</span> |
− | css = HF.get_style_defs( | + | <span class="n">css</span> <span class="o">=</span> <span class="n">HF</span><span class="o">.</span><span class="n">get_style_defs</span><span class="p">(</span><span class="s">'.highlight'</span><span class="p">)</span> |
− | return html,css | + | <span class="k">return</span> <span class="n">html</span><span class="p">,</span><span class="n">css</span> |
− | def code_to_html(code,language): | + | <span class="k">def</span> <span class="nf">code_to_html</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">language</span><span class="p">):</span> |
− | "" | + | <span class="sd">"""</span> |
− | + | <span class="sd"> Transform CODE into html and css (all in the same file)</span> | |
− | + | <span class="sd"> """</span> | |
− | # Obtain lexer and HtmlFormatter | + | <span class="c"># Obtain lexer and HtmlFormatter</span> |
− | L, HF = lexer_formatter(language) | + | <span class="n">L</span><span class="p">,</span> <span class="n">HF</span> <span class="o">=</span> <span class="n">lexer_formatter</span><span class="p">(</span><span class="n">language</span><span class="p">)</span> |
− | # Create fill html code | + | <span class="c"># Create fill html code</span> |
− | html = highlight(code,L,HF) | + | <span class="n">html</span> <span class="o">=</span> <span class="n">highlight</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">L</span><span class="p">,</span><span class="n">HF</span><span class="p">)</span> |
− | return html | + | <span class="k">return</span> <span class="n">html</span> |
− | import sys | + | <span class="kn">import</span> <span class="nn">sys</span> |
− | if __name__ == "__main__": | + | <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> |
− | "" | + | <span class="sd">"""</span> |
− | + | <span class="sd"> Command:</span> | |
− | + | <span class="sd"> </span> | |
− | + | <span class="sd"> ######################################</span> | |
− | + | <span class="sd"> </span> | |
− | + | <span class="sd"> python code2html INPUTFILE [CSS]</span> | |
− | + | <span class="sd"> </span> | |
− | + | <span class="sd"> ######################################</span> | |
− | + | <span class="sd"> </span> | |
− | + | <span class="sd"> INPUTFILE: name (with path) of the file to convert to html</span> | |
− | + | <span class="sd"> CSS: write "true" in order to obtain separate .html and .css files ("false" by default)</span> | |
− | + | <span class="sd"> """</span> | |
− | # Command line arguments | + | <span class="c"># Command line arguments</span> |
− | args = sys.argv | + | <span class="n">args</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> |
− | # Check command line arguments | + | <span class="c"># Check command line arguments</span> |
− | ncla = len(args) # number of command line arguments | + | <span class="n">ncla</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="c"># number of command line arguments</span> |
− | if ncla != 2 and ncla != 3 : | + | <span class="k">if</span> <span class="n">ncla</span> <span class="o">!=</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">ncla</span> <span class="o">!=</span> <span class="mi">3</span> <span class="p">:</span> |
− | raise TypeError("Invalid number of command line arguments.") | + | <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">"Invalid number of command line arguments."</span><span class="p">)</span> |
− | css_bool = False | + | <span class="n">css_bool</span> <span class="o">=</span> <span class="bp">False</span> |
− | if ncla == 3 and args[-1].lower() == "true": | + | <span class="k">if</span> <span class="n">ncla</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">"true"</span><span class="p">:</span> |
− | css_bool = True # Export css separately | + | <span class="n">css_bool</span> <span class="o">=</span> <span class="bp">True</span> <span class="c"># Export css separately</span> |
− | # Input file | + | <span class="c"># Input file</span> |
− | fname_code = sys.argv[1] # Name of the file containing the code to convert in html | + | <span class="n">fname_code</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="c"># Name of the file containing the code to convert in html</span> |
− | # Input file extension | + | <span class="c"># Input file extension</span> |
− | language = fname_code.split( | + | <span class="n">language</span> <span class="o">=</span> <span class="n">fname_code</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> |
− | # Output files | + | <span class="c"># Output files</span> |
− | fname_html = fname_code.split( | + | <span class="n">fname_html</span> <span class="o">=</span> <span class="n">fname_code</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">'.'</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">".html"</span> <span class="c"># Name of the file where the html code will be stored</span> |
− | fname_css = fname_code.split( | + | <span class="n">fname_css</span> <span class="o">=</span> <span class="n">fname_code</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">'.'</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">".css"</span> <span class="c"># Name of the file where the css code will be stored</span> |
− | # Save code into a unformatted string | + | <span class="c"># Save code into a unformatted string</span> |
− | code = load_file_as_sting(fname_code) | + | <span class="n">code</span> <span class="o">=</span> <span class="n">load_file_as_sting</span><span class="p">(</span><span class="n">fname_code</span><span class="p">)</span> |
− | if css_bool == False: # Convert to standalone html | + | <span class="k">if</span> <span class="n">css_bool</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span> <span class="c"># Convert to standalone html</span> |
− | html = code_to_html(code,language) | + | <span class="n">html</span> <span class="o">=</span> <span class="n">code_to_html</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">language</span><span class="p">)</span> |
− | else: # Convert to html and css separately | + | <span class="k">else</span><span class="p">:</span> <span class="c"># Convert to html and css separately</span> |
− | html,css = code_to_htmlcss(code,language) | + | <span class="n">html</span><span class="p">,</span><span class="n">css</span> <span class="o">=</span> <span class="n">code_to_htmlcss</span><span class="p">(</span><span class="n">code</span><span class="p">,</span><span class="n">language</span><span class="p">)</span> |
− | # Save html | + | <span class="c"># Save html</span> |
− | save_string_as_file(fname_html,html) | + | <span class="n">save_string_as_file</span><span class="p">(</span><span class="n">fname_html</span><span class="p">,</span><span class="n">html</span><span class="p">)</span> |
− | if css_bool == True: | + | <span class="k">if</span> <span class="n">css_bool</span> <span class="o">==</span> <span class="bp">True</span><span class="p">:</span> |
− | # Save css | + | <span class="c"># Save css</span> |
− | save_string_as_file(fname_css,css) | + | <span class="n">save_string_as_file</span><span class="p">(</span><span class="n">fname_css</span><span class="p">,</span><span class="n">css</span><span class="p">)</span> |
+ | </pre></div> | ||
+ | </body> | ||
+ | </html> |
Revision as of 16:29, 30 July 2015
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.lexers import CppLexer
from pygments.formatters import HtmlFormatter
# C++ extensions
cpp = ["cpp","cxx","cc","h"]
# Python extensions
py = ["py"]
def load_file_as_sting(fname):
"""
Open the file FNAME and save all its content in an unformatted string
"""
content = ""
with open(fname,'r') as f: # Open the file (read only)
content = f.read() # Read file and store it in an unformatted string
# The file is automatically closed
return content
def save_string_as_file(fname,string):
"""
Save the unformatted string STRING into the file FNAME
"""
with open(fname,'w') as f: # Open the file (write only)
f.write(string)
# The file is automatically closed
def lexer_formatter(language,css=False):
"""
Return the lexer for the appropriate language and the HTML formatter
"""
L = None
if language in py:
# Python Lexer
L = PythonLexer()
elif language in cpp:
# C++ Lexer
L = CppLexer()
else:
raise NameError("Invalid language.")
HF = HtmlFormatter(full=not css,style="monokai")
return L, HF
def code_to_htmlcss(code,language):
"""
Transform CODE into html and css (separate files)
"""
# Obtain lexer and HtmlFormatter
L, HF = lexer_formatter(language,css=True)
# Create html code
html = highlight(code,L,HF)
# Create css code
css = HF.get_style_defs('.highlight')
return html,css
def code_to_html(code,language):
"""
Transform CODE into html and css (all in the same file)
"""
# Obtain lexer and HtmlFormatter
L, HF = lexer_formatter(language)
# Create fill html code
html = highlight(code,L,HF)
return html
import sys
if __name__ == "__main__":
"""
Command:
######################################
python code2html INPUTFILE [CSS]
######################################
INPUTFILE: name (with path) of the file to convert to html
CSS: write "true" in order to obtain separate .html and .css files ("false" by default)
"""
# Command line arguments
args = sys.argv
# Check command line arguments
ncla = len(args) # number of command line arguments
if ncla != 2 and ncla != 3 :
raise TypeError("Invalid number of command line arguments.")
css_bool = False
if ncla == 3 and args[-1].lower() == "true":
css_bool = True # Export css separately
# Input file
fname_code = sys.argv[1] # Name of the file containing the code to convert in html
# Input file extension
language = fname_code.split('.')[-1]
# Output files
fname_html = fname_code.split('.')[0] + ".html" # Name of the file where the html code will be stored
fname_css = fname_code.split('.')[0] + ".css" # Name of the file where the css code will be stored
# Save code into a unformatted string
code = load_file_as_sting(fname_code)
if css_bool == False: # Convert to standalone html
html = code_to_html(code,language)
else: # Convert to html and css separately
html,css = code_to_htmlcss(code,language)
# Save html
save_string_as_file(fname_html,html)
if css_bool == True:
# Save css
save_string_as_file(fname_css,css)