mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/lib/translator.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 from lib.common import cfg
00024 from lib.common import latex
00025 from lib import is_
00026 import error
00027 
00028 
00029 markup_choice = cfg.get_value_from_file('MARKUP', 'USE')
00030 
00031 if markup_choice == 'latex':
00032     from lib.common.latex import MARKUP
00033 
00034 
00035 
00036 # --------------------------------------------------------------------------
00037 ##
00038 #   @brief Writes a table filled with the given [strings]
00039 #   @param size : (nb of lines, nb of columns)
00040 #   @param chosen_markup
00041 #   @param content : [strings]
00042 #   @options col_fmt : [int|<'l'|'c'|'r'>]
00043 #   @options : borders='all'
00044 #   @options : unit='inch' etc. (check the possibilities...)
00045 #   @return
00046 def create_table(size, content, **options):
00047     if markup_choice == 'latex':
00048         n_col = size[1]
00049         n_lin = size[0]
00050         result = ""
00051 
00052         length_unit = 'cm'
00053         if 'unit' in options:
00054             length_unit = options['unit']
00055 
00056         tabular_format = ""
00057         v_border = ""
00058         h_border = ""
00059         center = ""
00060         new_line_sep = "\\\\" + "\n"
00061 
00062         if 'center' in options:
00063             center = ">{\centering}"
00064             new_line_sep = "\\tabularnewline" + "\n"
00065 
00066         if 'borders' in options and options['borders'] == 'all':
00067             v_border = "|"
00068             h_border = "\\hline \n"
00069 
00070         col_fmt = ['c' for i in range(n_col)]
00071 
00072         #DBG
00073         #error.write_warning("type(options['col_fmt']) = " + type(options['col_fmt']))
00074 
00075         if 'col_fmt' in options and type(options['col_fmt']) == list \
00076             and len(options['col_fmt']) == n_col:
00077         #___
00078             for i in range(len(col_fmt)):
00079                 col_fmt[i] = options['col_fmt'][i]
00080 
00081         for i in range(len(col_fmt)):
00082             t = col_fmt[i]
00083             if is_.a_number(col_fmt[i]):
00084                 t = "p{" + str(col_fmt[i]) + " " + str(length_unit) + "}"
00085 
00086             tabular_format += v_border \
00087                               + center \
00088                               + t
00089 
00090         tabular_format += v_border
00091 
00092         result += "\\begin{tabular}{"+ tabular_format + "}" + "\n"
00093         result += h_border
00094 
00095         for i in range(int(n_lin)):
00096             for j in range(n_col):
00097                 result += str(content[i*n_col + j])
00098                 if j != n_col - 1:
00099                     result += "&" + "\n"
00100             if i != n_lin - 1:
00101                 result += new_line_sep + h_border
00102 
00103         result += new_line_sep + h_border
00104         result += "\end{tabular}" + "\n"
00105 
00106         return result
00107 
00108     else:
00109         raise error.NotImplementedYet("create_table using this markup : " \
00110                                         + markup_choice + " ")
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118