Difference between revisions of "Team:EPF Lausanne/Project/Modelling/Code"

 
(22 intermediate revisions by the same user not shown)
Line 20: Line 20:
 
   max-height:50%;
 
   max-height:50%;
 
}
 
}
.highlight > pre { background-color: #404040;}
+
.highlight > pre { background-color: #404040; display:block; height:600px; overflow:scroll}
 
.highlight .hll { background-color: #49483e }
 
.highlight .hll { background-color: #49483e }
 
.highlight  { background: #272822; color: #f8f8f2 }
 
.highlight  { background: #272822; color: #f8f8f2 }
Line 198: Line 198:
 
                 <div class="col-md-12 col-centered">
 
                 <div class="col-md-12 col-centered">
 
                     <h3>code2html</h3>
 
                     <h3>code2html</h3>
<p>The following Python code 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 the code in our Wiki is formatted using this script.
+
<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>
 
<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">PythonLexer</span>
Line 232: Line 232:
 
         <span class="c"># The file is automatically closed</span>
 
         <span class="c"># The file is automatically closed</span>
 
          
 
          
<span class="k">def</span> <span class="nf">lexer</span><span class="p">(</span><span class="n">language</span><span class="p">):</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">&quot;&quot;&quot;</span>
<span class="sd">    Return the lexer and the formatter for the appropriate language</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="sd">    &quot;&quot;&quot;</span>
 
      
 
      
Line 249: Line 249:
 
         <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="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="k">return</span> <span class="n">L</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>
  
  
Line 257: Line 259:
 
<span class="sd">    &quot;&quot;&quot;</span>
 
<span class="sd">    &quot;&quot;&quot;</span>
 
      
 
      
     <span class="c"># Obtain lexer</span>
+
     <span class="c"># Obtain lexer and HtmlFormatter</span>
     <span class="n">L</span> <span class="o">=</span> <span class="n">lexer</span><span class="p">(</span><span class="n">language</span><span class="p">)</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"># HtmlFormatter</span>
+
    <span class="n">HF</span> <span class="o">=</span> <span class="n">HtmlFormatter</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="c"># Create html code</span>
 
     <span class="c"># Create html code</span>
Line 276: Line 275:
 
<span class="sd">    &quot;&quot;&quot;</span>
 
<span class="sd">    &quot;&quot;&quot;</span>
 
      
 
      
     <span class="c"># Obtain lexer</span>
+
     <span class="c"># Obtain lexer and HtmlFormatter</span>
     <span class="n">L</span> <span class="o">=</span> <span class="n">lexer</span><span class="p">(</span><span class="n">language</span><span class="p">)</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"># HtmlFormatter</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="bp">True</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="c"># Create fill html code</span>
 
     <span class="c"># Create fill html code</span>
Line 343: Line 339:
 
</pre></div>
 
</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></div></div></div>
  
    <div class="second-section">
 
        <div class="container">
 
            <div class="row">
 
                <div class="col-md-12 col-centered">
 
                    <h3>ODE Generator</h3>
 
               
 
                <div class="highlight"><pre><span class="k">class</span> <span class="nc">Reaction</span><span class="p">:</span>
 
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reaction_</span><span class="p">,</span> <span class="n">cst_name_</span><span class="p">,</span> <span class="n">cst_value_</span><span class="p">,</span> <span class="n">description_</span><span class="p">):</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">reaction</span> <span class="o">=</span> <span class="n">reaction_</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">cst_name</span> <span class="o">=</span> <span class="n">cst_name_</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">cst_value</span> <span class="o">=</span> <span class="n">cst_value_</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="n">description_</span>
 
 
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
    <span class="k">return</span> <span class="s">&quot;{}</span><span class="se">\n</span><span class="s">{} = {}</span><span class="se">\n</span><span class="s">{}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reaction</span><span class="p">,</span>
 
    <span class="bp">self</span><span class="o">.</span><span class="n">cst_name</span><span class="p">,</span>
 
    <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cst_value</span><span class="p">),</span>
 
    <span class="bp">self</span><span class="o">.</span><span class="n">description</span><span class="p">)</span>
 
 
<span class="k">class</span> <span class="nc">ReactionParser</span><span class="p">:</span>
 
 
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_</span><span class="p">,</span> <span class="n">sep_</span><span class="o">=</span><span class="s">&quot;:&quot;</span><span class="p">,</span> <span class="n">comment_</span><span class="o">=</span><span class="s">&quot;#&quot;</span><span class="p">):</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">sep</span> <span class="o">=</span> <span class="n">sep_</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">comment</span> <span class="o">=</span> <span class="n">comment_</span>
 
 
<span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span> <span class="c"># TODO: OPEN SAFELY</span>
 
 
<span class="bp">self</span><span class="o">.</span><span class="n">reactions</span> <span class="o">=</span> <span class="p">[]</span>
 
 
<span class="bp">self</span><span class="o">.</span><span class="n">_load</span><span class="p">()</span>
 
 
<span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
<span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
 
<span class="k">def</span> <span class="nf">_load</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="p">:</span>
 
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="ow">or</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;&quot;</span><span class="p">:</span> <span class="c"># Check for empty lines</span>
 
<span class="k">continue</span>
 
 
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment</span><span class="p">:</span> <span class="c"># Check for comments</span>
 
<span class="k">continue</span>
 
 
<span class="n">splitted</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)</span>
 
 
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">splitted</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">:</span>
 
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;ReactionParser: problem with the input file (not enough arguments)&#39;</span><span class="p">)</span>
 
 
<span class="n">reaction</span> <span class="o">=</span> <span class="n">Reaction</span><span class="p">(</span><span class="n">splitted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(),</span>
 
<span class="n">splitted</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(),</span>
 
<span class="nb">float</span><span class="p">(</span><span class="n">splitted</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()),</span>
 
<span class="n">splitted</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
 
 
<span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reaction</span><span class="p">)</span>
 
 
<span class="k">def</span> <span class="nf">print_reactions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
<span class="k">for</span> <span class="n">reaction</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="p">:</span>
 
<span class="k">print</span><span class="p">(</span><span class="n">reaction</span><span class="p">)</span>
 
<span class="k">print</span><span class="p">()</span>
 
 
<span class="k">def</span> <span class="nf">reaction_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
<span class="n">kin</span> <span class="o">=</span> <span class="p">[]</span>
 
 
<span class="k">for</span> <span class="n">reaction</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="p">:</span>
 
<span class="n">splitted</span> <span class="o">=</span> <span class="n">reaction</span><span class="o">.</span><span class="n">reaction</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;-&gt;&quot;</span><span class="p">)</span>
 
 
<span class="n">left</span> <span class="o">=</span> <span class="n">splitted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
 
<span class="n">right</span> <span class="o">=</span> <span class="n">splitted</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">strip</span><span class="p">()</span>
 
 
<span class="n">kin</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="p">([</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">],</span> <span class="p">(</span><span class="n">reaction</span><span class="o">.</span><span class="n">cst_name</span><span class="p">,</span> <span class="n">reaction</span><span class="o">.</span><span class="n">description</span><span class="p">,</span> <span class="n">reaction</span><span class="o">.</span><span class="n">cst_value</span><span class="p">)</span> <span class="p">)</span> <span class="p">)</span>
 
 
<span class="k">return</span> <span class="n">kin</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="n">myParser</span> <span class="o">=</span> <span class="n">ReactionParser</span><span class="p">(</span><span class="s">&quot;reactions.dat&quot;</span><span class="p">)</span>
 
 
<span class="c">#myParser.load()</span>
 
<span class="n">myParser</span><span class="o">.</span><span class="n">print_reactions</span><span class="p">()</span>
 
 
<span class="n">kin</span> <span class="o">=</span> <span class="n">myParser</span><span class="o">.</span><span class="n">reaction_table</span><span class="p">()</span>
 
 
<span class="k">print</span><span class="p">(</span><span class="n">kin</span><span class="p">)</span>
 
</pre></div>
 
  
                   
 
                </div>
 
            </div>
 
        </div>
 
    </div>
 
  
  

Latest revision as of 16:57, 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