Difference between revisions of "Team:EPF Lausanne/Software"

(Prototype team page)
 
Line 1: Line 1:
{{EPF_Lausanne}}
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<html>
+
  <head>
 +
<style type="text/css">
  
<h2>Software</h2>
+
  #contentSub, #footer-box, #catlinks, #search-controls,
 +
  #p-logo, .printfooter, .firstHeading, .visualClear {
 +
      display: none;
 +
  }
 +
  #globalWrapper, #content {   
 +
    width: 100%;
 +
    height: 100%;
 +
    border: 0px;
 +
    background-color: #669999;
 +
    margin: 0px;
 +
    padding: 0px;
 +
    font-size: 100%;
 +
  }
 +
img.resize50{
 +
  max-width:50%;
 +
  max-height:50%;
 +
}
 +
.highlight > pre { background-color: #404040;  display:block; height:600px; overflow:scroll}
 +
.highlight .hll { background-color: #49483e }
 +
.highlight  { background: #272822; color: #f8f8f2 }
 +
.highlight .c { color: #75715e } /* Comment */
 +
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
 +
.highlight .k { color: #66d9ef } /* Keyword */
 +
.highlight .l { color: #ae81ff } /* Literal */
 +
.highlight .n { color: #f8f8f2 } /* Name */
 +
.highlight .o { color: #f92672 } /* Operator */
 +
.highlight .p { color: #f8f8f2 } /* Punctuation */
 +
.highlight .cm { color: #75715e } /* Comment.Multiline */
 +
.highlight .cp { color: #75715e } /* Comment.Preproc */
 +
.highlight .c1 { color: #75715e } /* Comment.Single */
 +
.highlight .cs { color: #75715e } /* Comment.Special */
 +
.highlight .gd { color: #f92672 } /* Generic.Deleted */
 +
.highlight .ge { font-style: italic } /* Generic.Emph */
 +
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
 +
.highlight .gs { font-weight: bold } /* Generic.Strong */
 +
.highlight .gu { color: #75715e } /* Generic.Subheading */
 +
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
 +
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
 +
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
 +
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
 +
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
 +
.highlight .kt { color: #66d9ef } /* Keyword.Type */
 +
.highlight .ld { color: #e6db74 } /* Literal.Date */
 +
.highlight .m { color: #ae81ff } /* Literal.Number */
 +
.highlight .s { color: #e6db74 } /* Literal.String */
 +
.highlight .na { color: #a6e22e } /* Name.Attribute */
 +
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
 +
.highlight .nc { color: #a6e22e } /* Name.Class */
 +
.highlight .no { color: #66d9ef } /* Name.Constant */
 +
.highlight .nd { color: #a6e22e } /* Name.Decorator */
 +
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
 +
.highlight .ne { color: #a6e22e } /* Name.Exception */
 +
.highlight .nf { color: #a6e22e } /* Name.Function */
 +
.highlight .nl { color: #f8f8f2 } /* Name.Label */
 +
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
 +
.highlight .nx { color: #a6e22e } /* Name.Other */
 +
.highlight .py { color: #f8f8f2 } /* Name.Property */
 +
.highlight .nt { color: #f92672 } /* Name.Tag */
 +
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
 +
.highlight .ow { color: #f92672 } /* Operator.Word */
 +
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
 +
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
 +
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
 +
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
 +
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
 +
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
 +
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
 +
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
 +
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
 +
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
 +
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
 +
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
 +
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
 +
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
 +
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
 +
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
 +
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
 +
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
 +
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
 +
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
 +
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
 +
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
  
  
<div class="highlightBox">
+
</style>
<h4>Note</h4>
+
    <!--
<p>In order to be considered for the <a href="https://2015.igem.org/Judging/Awards#SpecialPrizes">Best Software Tool award</a>, you must fill out this page.</p>
+
    Stone Template
</div>
+
    http://www.templatemo.com/preview/templatemo_452_stone
 +
    -->
  
 +
    <!-- Latest compiled and minified CSS -->
 +
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
 +
   
 +
    <!-- Bootstrap Stylesheet -->
 +
    <link rel="stylesheet" href="https://2015.igem.org/Team:EPF_Lausanne/Test/css/bootstrap?action=raw&amp;ctype=text/css" type="text/css">
 +
   
 +
    <!-- FontAwesome Icons -->
 +
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
 +
   
 +
    <!-- Normailize Stylesheet -->
 +
    <link rel="stylesheet" href="https://2015.igem.org/Team:EPF_Lausanne/Test/css/normalize?action=raw&amp;ctype=text/css" type="text/css">
  
<p>Regardless of the topic, iGEM projects often create or adapt computational tools to move the project forward. Because they are born out of a direct practical need, these software tools (or new computational methods) can be surprisingly useful for other teams. Without necessarily being big or complex, they can make the crucial difference to a project's success. This award tries to find and honour such "nuggets" of computational work.</p>
+
    <!-- Main Styles -->
 +
    <link rel="stylesheet" href="https://2015.igem.org/Team:EPF_Lausanne/Test/css/templatemo-style?action=raw&amp;ctype=text/css" type="text/css">
  
<p>
+
    <!-- Footer -->
If you are working on software as your main project, please join the software track. If you are creating software as an addition to your main project, please apply for this award.
+
    <link rel="stylesheet" href="https://2015.igem.org/Team:EPF_Lausanne/css/footer?action=raw&amp;ctype=text/css" type="text/css">
</p>
+
  
<p>
+
    <script src="https://2015.igem.org/Team:EPF_Lausanne/Test/js/mordernizr-2.6.2?action=raw&amp;ctype=text/js"></script>
Here are a few examples from previous teams:
+
</p>
+
<ul>
+
<li><a href="https://2013.igem.org/Team:TU-Munich/Results/Software">TU Munich 2013</a></li>
+
<li><a href="https://2014.igem.org/Team:Heidelberg/Software">Heidelberg 2014</a></li>
+
<li><a href="https://2014.igem.org/Team:Aachen/Project/Measurement_Device#Software">Aachen 2014</a></li>
+
</ul>
+
  
</div>
+
</head>
 +
<body  class="loading">
 +
    <!--[if lt IE 7]>
 +
    <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
 +
    <![endif]-->
  
 +
    <!-- HEADER -->
 +
    <div class="site-header">
 +
        <div class="container">
 +
            <div class="row">
 +
                <nav class="navbar navbar-default navbar-static-top">
 +
                    <div class="navbar-header">
 +
                        <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#main-menu">
 +
                            <span class="sr-only">Toggle navigation</span>
 +
                            <i class="fa fa-bars"></i>
 +
                        </button>
 +
                        <a href="https://2015.igem.org/Team:EPF_Lausanne"><img src="https://static.igem.org/mediawiki/2015/9/95/EPF_Lausanne_BIOLOGIC_BLANC.png"></a>
 +
                    </div>
 +
                    <div class="collapse navbar-collapse" id="main-menu">
 +
                        <ul class="nav navbar-nav navbar-right">
 +
                            <li><a href="https://2015.igem.org/Team:EPF_Lausanne">Home</a></li>
 +
                            <li class="dropdown">
 +
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Project <span class="caret"></span></a>
 +
                            <ul class="dropdown-menu">
 +
                                <li><a href="https://2015.igem.org/Team_EPF_Lausanne/Project/Background">Background</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Project/Description">Description</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Project/Applications">Applications</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Project/Modelling">Modelling</a></li>
 +
                            </ul>
 +
                            </li>
 +
                            <li class="dropdown">
 +
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Achievements <span class="caret"></span></a>
 +
                            <ul class="dropdown-menu">
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Achievements/Vivo">In vivo</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Achievements/Silico">In silico</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Achievements/Judging">Judging</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Achievements/Parts">Parts</a></li>
 +
                            </ul>
 +
                            </li>
 +
                            <li class="dropdown">
 +
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Policy and Practices <span class="caret"></span></a>
 +
                            <ul class="dropdown-menu">
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Policy/Ethic">Ethics</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Policy/Safety">Safety</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Policy/Human">Human Practices</a></li>
 +
                            </ul>
 +
                            </li>
 +
                            <li class="dropdown">
 +
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Notebook <span class="caret"></span></a>
 +
                            <ul class="dropdown-menu">
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Notebook/Ecoli">E. Coli</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Notebook/Yeast">Yeast</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Notebook/Protocols">Protocols</a></li>
 +
                            </ul>
 +
                            </li>
 +
                            <li class="dropdown">
 +
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Team <span class="caret"></span></a>
 +
                            <ul class="dropdown-menu">
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Team/Meet">Meet us</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Timeline">Timeline</a></li>
 +
                                <li><a href="https://2015.igem.org/Team:EPF_Lausanne/Team/Aknowledgements">Aknowledgements</a></li>
 +
                            </ul>
 +
                            </li>
 +
                        </ul>
 +
                    </div>
 +
                </nav>       
 +
            </div>
 +
        </div>
 +
    </div> <!-- .site-header -->
 +
 +
    <div class="page-h promotion">
 +
        <div class="container">
 +
            <div class="row">
 +
                <div class="col-md-12 text-center">
 +
                    <h3>Code</h3>
 +
                </div>
 +
            </div>
 +
        </div>
 +
    </div>
 +
 +
<div class="second-section">
 +
        <div class="container">
 +
            <div class="row">
 +
                <div class="col-md-12 col-centered">
 +
                    <h3>code2html</h3>
 +
<p>The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on <a href="http://pygments.org/" target="blank">Pygment</a>, a Python syntax highlighter. All code in our Wiki is formatted using this script.
 +
<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">&quot;cpp&quot;</span><span class="p">,</span><span class="s">&quot;cxx&quot;</span><span class="p">,</span><span class="s">&quot;cc&quot;</span><span class="p">,</span><span class="s">&quot;h&quot;</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">&quot;py&quot;</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">&quot;&quot;&quot;</span>
 +
<span class="sd">    Open the file FNAME and save all its content in an unformatted string</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <span class="n">content</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
 +
   
 +
    <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">&#39;r&#39;</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>
 +
        <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>
 +
        <span class="c"># The file is automatically closed</span>
 +
   
 +
    <span class="k">return</span> <span class="n">content</span>
 +
   
 +
<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">&quot;&quot;&quot;</span>
 +
<span class="sd">    Save the unformatted string STRING into the file FNAME</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <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">&#39;w&#39;</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>
 +
        <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>
 +
        <span class="c"># The file is automatically closed</span>
 +
       
 +
<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">&quot;&quot;&quot;</span>
 +
<span class="sd">    Return the lexer for the appropriate language and the HTML formatter</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <span class="n">L</span> <span class="o">=</span> <span class="bp">None</span>
 +
   
 +
    <span class="k">if</span> <span class="n">language</span> <span class="ow">in</span> <span class="n">py</span><span class="p">:</span>
 +
        <span class="c"># Python Lexer</span>
 +
        <span class="n">L</span> <span class="o">=</span> <span class="n">PythonLexer</span><span class="p">()</span>
 +
       
 +
    <span class="k">elif</span> <span class="n">language</span> <span class="ow">in</span> <span class="n">cpp</span><span class="p">:</span>
 +
        <span class="c"># C++ Lexer</span>
 +
        <span class="n">L</span> <span class="o">=</span> <span class="n">CppLexer</span><span class="p">()</span>
 +
    <span class="k">else</span><span class="p">:</span>
 +
        <span class="k">raise</span> <span class="ne">NameError</span><span class="p">(</span><span class="s">&quot;Invalid language.&quot;</span><span class="p">)</span>
 +
       
 +
    <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="n">css</span><span class="p">,</span><span class="n">style</span><span class="o">=</span><span class="s">&quot;monokai&quot;</span><span class="p">)</span>
 +
       
 +
    <span class="k">return</span> <span class="n">L</span><span class="p">,</span> <span class="n">HF</span>
 +
 +
 +
<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">&quot;&quot;&quot;</span>
 +
<span class="sd">    Transform CODE into html and css (separate files)</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <span class="c"># Obtain lexer and HtmlFormatter</span>
 +
    <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>
 +
   
 +
    <span class="c"># Create html code</span>
 +
    <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>
 +
   
 +
    <span class="c"># Create css code</span>
 +
    <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">&#39;.highlight&#39;</span><span class="p">)</span>
 +
   
 +
    <span class="k">return</span> <span class="n">html</span><span class="p">,</span><span class="n">css</span>
 +
   
 +
<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">&quot;&quot;&quot;</span>
 +
<span class="sd">    Transform CODE into html and css (all in the same file)</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <span class="c"># Obtain lexer and HtmlFormatter</span>
 +
    <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="c"># Create fill html code</span>
 +
    <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>
 +
   
 +
    <span class="k">return</span> <span class="n">html</span>
 +
 +
<span class="kn">import</span> <span class="nn">sys</span>
 +
 +
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
 +
    <span class="sd">&quot;&quot;&quot;</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 &quot;true&quot; in order to obtain separate .html and .css files (&quot;false&quot; by default)</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
   
 +
    <span class="c"># Command line arguments</span>
 +
    <span class="n">args</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span>
 +
   
 +
    <span class="c"># Check command line arguments</span>
 +
    <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>
 +
   
 +
    <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>
 +
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;Invalid number of command line arguments.&quot;</span><span class="p">)</span>
 +
       
 +
    <span class="n">css_bool</span> <span class="o">=</span> <span class="bp">False</span>
 +
   
 +
    <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">&quot;true&quot;</span><span class="p">:</span>
 +
        <span class="n">css_bool</span> <span class="o">=</span> <span class="bp">True</span> <span class="c"># Export css separately</span>
 +
   
 +
    <span class="c"># Input file</span>
 +
    <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>
 +
   
 +
    <span class="c"># Input file extension</span>
 +
    <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">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
 +
   
 +
    <span class="c"># Output files</span>
 +
    <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">&#39;.&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot;.html&quot;</span> <span class="c"># Name of the file where the html code will be stored</span>
 +
    <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">&#39;.&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot;.css&quot;</span> <span class="c"># Name of the file where the css code will be stored</span>
 +
   
 +
    <span class="c"># Save code into a unformatted string</span>
 +
    <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>
 +
   
 +
    <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>
 +
        <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>
 +
    <span class="k">else</span><span class="p">:</span> <span class="c"># Convert to html and css separately</span>
 +
        <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>
 +
   
 +
    <span class="c"># Save html</span>
 +
    <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>
 +
   
 +
    <span class="k">if</span> <span class="n">css_bool</span> <span class="o">==</span> <span class="bp">True</span><span class="p">:</span>
 +
        <span class="c"># Save css</span>
 +
        <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>
 +
 +
<center><div>
 +
<a href="https://static.igem.org/mediawiki/2015/b/bf/Code2html.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span></a></div></center>
 +
</div></div></div></div>
 +
 +
 +
 +
 +
    <div class="fourth-section">
 +
        <div class="container">
 +
            <div class="row">
 +
                <div class="col-md-12 text-center">
 +
                    <h2>Still under construction</h2>
 +
                </div>
 +
            </div>
 +
        </div>
 +
    </div>
 +
    <!-- Footer -->
 +
    <footer class="footer-distributed">
 +
 +
            <div class="footer-left">
 +
 +
                <img class="resize50" src="https://static.igem.org/mediawiki/2015/4/4e/EPF_Lausanne_iGem_Logo_Blue.png" alt="">
 +
 +
            </div>
 +
 +
            <div class="footer-center">
 +
 +
                <div>
 +
                    <i class="fa fa-map-marker"></i>
 +
                    <p><span>Route Cantonale</span> Lausanne, Suisse</p>
 +
                </div>
 +
 +
                <div>
 +
                    <i class="fa fa-envelope"></i>
 +
                    <p><a href="mailto:iGEM2015dreamteam@groupes.epfl.ch">Contact us</a></p>
 +
                </div>
 +
 +
            </div>
 +
 +
            <div class="footer-right">
 +
 +
                <p class="footer-company-about">
 +
                    <span>About us</span>
 +
                    Team of a dozen biotechnology students that will attempt to change the world
 +
                </p>
 +
 +
                <div class="footer-icons">
 +
 +
                    <a href="https://www.facebook.com/pages/IGEM-EPFL-2015/1598472927106907" target="_blank"><i class="fa fa-facebook"></i></a>
 +
                    <a href="https://instagram.com/igem2015epfl/" target="_blank"><i class="fa fa-instagram"></i></a>
 +
                    <a href="https://twitter.com/EPFL_iGEM/" target="_blank"><i class="fa fa-twitter"></i></a>
 +
                </div>
 +
 +
            </div>
 +
 +
        </footer>
 +
 +
    <script src="https://2015.igem.org/Team:EPF_Lausanne/Test/js/jquery?action=raw&amp;ctype=text/js"></script>
 +
    <script src="https://2015.igem.org/Team:EPF_Lausanne/Test/js/bootstrap?action=raw&amp;ctype=text/js"></script>
 +
    <script src="https://2015.igem.org/Team:EPF_Lausanne/Test/js/plugins?action=raw&amp;ctype=text/js"></script>
 +
    <script src="https://2015.igem.org/Team:EPF_Lausanne/Test/js/main?action=raw&amp;ctype=text/js"></script>   
 +
</body>
 
</html>
 
</html>

Revision as of 17:09, 30 July 2015

Code

code2html

The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on Pygment, a Python syntax highlighter. All code in our Wiki is formatted using this script.

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=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)

Still under construction