]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* add transitional beginboxx template and move some places to the new syntax
[citadel.git] / webcit / subst.c
index 3449489ab78e2e22c11db36ce6f662ed2163fe28..a1879ba9d39628b7b070502b7a783e04b1dfb2f4 100644 (file)
@@ -473,15 +473,30 @@ void StrBufAppendTemplate(StrBuf *Target,
                          int nArgs, 
                          WCTemplateToken *Tokens,
                          void *Context, int ContextType,
-                         StrBuf *Source, int FormatTypeIndex)
+                         const StrBuf *Source, int FormatTypeIndex)
 {
+        struct wcsession *WCC;
+       StrBuf *Buf;
        char EscapeAs = ' ';
 
-
+       if ((FormatTypeIndex < Tokens->nParameters) &&
+           (Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
+           (Tokens->Params[FormatTypeIndex]->len == 1)) {
+               EscapeAs = *Tokens->Params[FormatTypeIndex]->Start;
+       }
 
        switch(EscapeAs)
        {
-
+       case 'H':
+               WCC = WC;
+               Buf = NewStrBufPlain(NULL, StrLength(Buf));
+               StrBuf_RFC822_to_Utf8(Buf, 
+                                     Source, 
+                                     (WCC!=NULL)? WCC->DefaultCharset : NULL, 
+                                     NULL);
+               StrEscAppend(Target, Buf, NULL, 0, 0);
+               FreeStrBuf(&Buf);
+               break;
        case 'X':
                StrEscAppend(Target, Source, NULL, 0, 0);
                break;
@@ -491,6 +506,54 @@ void StrBufAppendTemplate(StrBuf *Target,
 }
 
 
+void GetTemplateTokenString(WCTemplateToken *Tokens,
+                           int N, 
+                           const char **Value, 
+                           long *len)
+{
+       StrBuf *Buf;
+
+       if (Tokens->nParameters < N) {
+               *Value = "";
+               *len = 0;
+               return;
+       }
+
+       switch (Tokens->Params[N]->Type) {
+
+       case TYPE_STR:
+               *Value = Tokens->Params[N]->Start;
+               *len = Tokens->Params[N]->len;
+               break;
+       case TYPE_BSTR:
+               Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start, 
+                                     Tokens->Params[N]->len);
+               *Value = ChrPtr(Buf);
+               *len = StrLength(Buf);
+               break;
+       case TYPE_PREFSTR:
+               get_PREFERENCE(
+                       Tokens->Params[N]->Start, 
+                       Tokens->Params[N]->len, 
+                       &Buf);
+               *Value = ChrPtr(Buf);
+               *len = StrLength(Buf);
+               break;
+       case TYPE_LONG:
+       case TYPE_PREFINT:
+               break; ///todo: string to text?
+       case TYPE_GETTEXT:
+               *Value = _(Tokens->Params[N]->Start);
+               *len = strlen(*Value);
+               break;
+       default:
+               break;
+//todo log error
+       }
+}
+
+
+
 /**
  * \brief Print the value of a variable
  * \param keyname get a key to print
@@ -643,11 +706,33 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
        TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
        char quote = '\0';
        
+
+       Parm->Type = TYPE_STR;
+
        /* Skip leading whitespaces */
        while ((*pch == ' ' )||
               (*pch == '\t')||
               (*pch == '\r')||
               (*pch == '\n')) pch ++;
+
+       if (*pch == ':') {
+               Parm->Type = TYPE_PREFSTR;
+               pch ++;
+       }
+       else if (*pch == ';') {
+               Parm->Type = TYPE_PREFINT;
+               pch ++;
+       }
+       else if (*pch == '_') {
+               Parm->Type = TYPE_GETTEXT;
+               pch ++;
+       }
+       else if (*pch == 'B') {
+               Parm->Type = TYPE_BSTR;
+               pch ++;
+       }
+
+
        if (*pch == '"')
                quote = '"';
        else if (*pch == '\'')
@@ -655,7 +740,6 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
        if (quote != '\0') {
                pch ++;
                pchs = pch;
-               Parm->Type = TYPE_STR;
                while (pch <= pe &&
                       ((*pch != quote) ||
                        ( (pch > pchs) && (*(pch - 1) == '\\'))
@@ -903,6 +987,14 @@ int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmp
                return (state != 0)?Token->Params[1]->lvalue:0;
            
        Cond = (ConditionalStruct *) Token->PreEval;
+       if (Cond == NULL) {
+               lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n", 
+                       Token->Params[0]->Start,
+                       ChrPtr(pTmpl->FileName),
+                       Token->Line,
+                       ChrPtr(Token->FlatToken));
+               return 1;
+       }
 
        if (Token->nParameters < Cond->nParams) {
                lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n", 
@@ -965,6 +1057,20 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, voi
                                                     Token->Params[4]->len,
                                                     0);
                }
+               else  {
+                       lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n", 
+                               Token->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               ChrPtr(Token->FlatToken));
+                       StrBufAppendPrintf(
+                               Target, 
+                               "<pre>\nConditional [%s] (in '%s' line %ld); needs 6 Params!\n[%s]\n</pre>\n", 
+                               Token->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               ChrPtr(Token->FlatToken));
+               }
                break;
        case SV_SUBTEMPL:
                if (Token->nParameters == 1)
@@ -1543,25 +1649,53 @@ void RegisterConditional(const char *Name, long len,
                         int ContextRequired)
 {
        ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
+       Cond->PlainName = Name;
        Cond->nParams = nParams;
        Cond->CondF = CondF;
        Cond->ContextRequired = ContextRequired;
        Put(Conditionals, Name, len, Cond, NULL);
 }
 
+
+void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
+}
+int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       StrBuf *TokenText = (StrBuf*) Context;
+       const char *CompareToken;
+       long len;
+
+       GetTemplateTokenString(Tokens, 2, &CompareToken, &len);
+       return strcmp(ChrPtr(TokenText), CompareToken) == 0;
+}
+
+
 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
+       StrBuf *Headline;
        if (nArgs == 2) {
-               StrBuf *Headline = NewStrBuf();
-               DoTemplate(Tokens->Params[1]->Start, 
-                          Tokens->Params[1]->len,
-                          Headline, 
-                          Context, 
-                          ContextType);
-               SVPutBuf("BOXTITLE", Headline, 0);
+               if (Tokens->Params[1]->Type == TYPE_STR) {
+                       Headline = NewStrBuf();
+                       DoTemplate(Tokens->Params[1]->Start, 
+                                  Tokens->Params[1]->len,
+                                  Headline, 
+                                  Context, 
+                                  ContextType);
+               }
+               else {
+                       const char *Ch;
+                       long len;
+                       GetTemplateTokenString(Tokens, 
+                                              1,
+                                              &Ch,
+                                              &len);
+                       Headline = NewStrBufPlain(Ch, len);
+               }
        }
        
-       DoTemplate(HKEY("beginbox"), Target, Context, ContextType);
+       DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
        DoTemplate(Tokens->Params[0]->Start, 
                   Tokens->Params[0]->len,
                   Target, 
@@ -1570,6 +1704,7 @@ void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Con
        DoTemplate(HKEY("endbox"), Target, Context, ContextType);
 }
 
+
 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBuf **TabNames;
@@ -1610,10 +1745,12 @@ void
 InitModule_SUBST
 (void)
 {
+       RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
        RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
        RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
        RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
        RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
+       RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
 }
 
 /*@}*/