basix_doc 0.1
/Users/mourrain/Devel/mmx/basix/src/document.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : document.cpp
00004 * DESCRIPTION: Expressions for (TeXmacs) documents with special markup
00005 * COPYRIGHT  : (C) 2009  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license and comes WITHOUT
00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
00009 * If you don't have this file, write to the Free Software Foundation, Inc.,
00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00011 ******************************************************************************/
00012 
00013 #include <basix/document.hpp>
00014 #include <basix/compound.hpp>
00015 namespace mmx {
00016 
00017 generic
00018 documentify (const string& s) {
00019   vector<string> v= tokenize (s, "\n");
00020   if (N(v) == 0) return gen ("$concat");
00021   if (N(v) == 1) return generic (v[0]);
00022   vector<generic> r= vec<generic> (v[0]);
00023   for (nat i=1; i<N(v); i++) r << generic ("$lf") << generic (v[i]);
00024   return gen ("$concat", r);
00025 }
00026 
00027 document::document (const char* s): rep (documentify (s)) {}
00028 document::document (const string& s): rep (documentify (s)) {}
00029 
00030 vector<generic>
00031 as_generics (const vector<document>& v) {
00032   vector<generic> r= fill<generic> (N(v));
00033   for (nat i=0; i<N(v); i++) r[i]= as_generic (v[i]);
00034   return r;
00035 }
00036 
00037 vector<document>
00038 as_documents (const vector<generic>& v) {
00039   vector<document> r= fill<document> (N(v));
00040   for (nat i=0; i<N(v); i++) r[i]= as_document (v[i]);
00041   return r;
00042 }
00043 
00044 document
00045 make_text (const generic& x) {
00046   return as_document (gen ("$text", x));
00047 }
00048 
00049 document
00050 make_text (const document& x) {
00051   return make_text (as_generic (x));
00052 }
00053 
00054 document
00055 make_math (const generic& x) {
00056   return as_document (gen ("$math", x));
00057 }
00058 
00059 document
00060 make_math (const document& x) {
00061   return make_math (as_generic (x));
00062 }
00063 
00064 document
00065 make_row (const vector<document>& v) {
00066   vector<generic> r= as_generics (v);
00067   return as_document (gen (GEN_ROW, r));
00068 }
00069 
00070 document
00071 make_document (const vector<document>& v, bool block= false) {
00072   //mmout << "v= " << v << indent << lf;
00073   vector<generic> r;
00074   if (block) r << generic ("$cr");
00075   for (nat i=0; i<N(v); i++) {
00076     generic x= as_generic (v[i]);
00077     //mmout << "v[" << i << "] = " << x << ": " << type_name (x) << lf;
00078     if (is_func (x, GEN_ROW)) {
00079       vector<document> a= as_documents (cdr (compound_to_vector (x)));
00080       r << as_generic (make_document (a));
00081       if (i != N(v) - 1) r << generic ("$lf");
00082     }
00083     else r << x;
00084   }
00085   //mmout << unindent;
00086   //mmout << "r= " << r << lf;
00087   if (N (r) == 1) return as_document (r[0]);
00088   else return as_document (gen ("$concat", r));
00089 }
00090 
00091 document
00092 make_inline (const vector<document>& v) {
00093   return make_document (v);
00094 }
00095 
00096 document
00097 make_block (const vector<document>& v) {
00098   return make_document (v, true);
00099 }
00100 
00101 document
00102 make_texmacs (const string& fun, const vector<document>& v) {
00103   return as_document (gen ("tm$" * fun, as_generics (v)));
00104 }
00105 
00106 document
00107 make_texmacs (const literal& fun, const vector<document>& v) {
00108   return as_document (gen ("tm$" * as_string (fun), as_generics (v)));
00109 }
00110 
00111 } // namespace mmx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines