Changeset 21752 for main/trunk


Ignore:
Timestamp:
2010-03-02T12:16:10+13:00 (14 years ago)
Author:
mdewsnip
Message:

Added new "htmlsafe:", "xmlsafe:", and "truncate(X):" (truncate metadata value to X characters) format statement modifiers. By Michael Dewsnip at DL Consulting Ltd.

Location:
main/trunk/greenstone2/runtime-src/src/recpt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/formattools.cpp

    r20756 r21752  
    462462
    463463
     464static void get_truncate_options (text_t &instring, metadata_t &metaoption)
     465{
     466  assert (instring.size() > ((text_t) "truncate").size());
     467  if (instring.size() <= ((text_t) "truncate").size()) return;
     468  text_t meta, com;
     469  bool inbraces = false;
     470  bool foundcolon = false;
     471  text_t::const_iterator here = instring.begin() + ((text_t) "truncate").size();
     472  text_t::const_iterator end = instring.end();
     473  while (here != end) {
     474    if (foundcolon) meta.push_back (*here);
     475    else if (*here == '(') inbraces = true;
     476    else if (*here == ')') inbraces = false;
     477    else if (*here == ':' && !inbraces) foundcolon = true;   
     478    else com.push_back (*here);
     479    ++here;
     480  }
     481
     482  instring = meta;
     483
     484  if (!com.empty())
     485  {
     486    metaoption.siblingoptions = com;
     487  }
     488  else
     489  {
     490    // Default is 100 characters if not specified
     491    metaoption.siblingoptions = "100";
     492  }
     493}
     494
     495
    464496
    465497static void parse_meta (text_t &meta, metadata_t &metaoption,
    466498            text_tset &metadata, bool &getParents) {
    467499
    468   if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
    469     metaoption.metacommand |= mCgiSafe;
    470     meta = substr (meta.begin()+8, meta.end());
    471   }
    472   if (meta.size() > 7 && (substr(meta.begin(), meta.begin()+7) == "format:")) {   
    473     metaoption.metacommand |= mSpecial;
    474     meta = substr (meta.begin()+7, meta.end());
     500  // Look for the various format statement modifiers
     501  // This needs to be done in a loop otherwise not all combinations will be accepted, but actually the order
     502  //   is irrelevant because this is not stored in metaoption.metacommand anyway
     503  bool keep_trying = true;
     504  while (keep_trying)
     505  {
     506    keep_trying = false;
     507
     508    if (meta.size() > ((text_t) "cgisafe:").size() && starts_with(meta, "cgisafe:"))
     509    {
     510      metaoption.metacommand |= mCgiSafe;
     511      meta = substr(meta.begin() + ((text_t) "cgisafe:").size(), meta.end());
     512      keep_trying = true;
     513    }
     514    if (meta.size() > ((text_t) "format:").size() && starts_with(meta, "format:"))
     515    {   
     516      metaoption.metacommand |= mSpecial;
     517      meta = substr(meta.begin() + ((text_t) "format:").size(), meta.end());
     518      keep_trying = true;
     519    }
     520
     521    // New "truncate" special formatting option
     522    if (meta.size() > ((text_t) "truncate").size() && starts_with(meta, "truncate"))  // No colons due to truncate(X)
     523    {
     524      metaoption.metacommand |= mTruncate;
     525      get_truncate_options (meta, metaoption);
     526      keep_trying = true;
     527    }
     528    // New "htmlsafe" special formatting option
     529    if (meta.size() > ((text_t) "htmlsafe:").size() && starts_with(meta, "htmlsafe:"))
     530    {
     531      metaoption.metacommand |= mHTMLSafe;
     532      meta = substr(meta.begin() + ((text_t) "htmlsafe:").size(), meta.end());
     533      keep_trying = true;
     534    }
     535    // New "xmlsafe" special formatting option
     536    if (meta.size() > ((text_t) "xmlsafe:").size() && starts_with(meta, "xmlsafe:"))
     537    {
     538      metaoption.metacommand |= mXMLSafe;
     539      meta = substr(meta.begin() + ((text_t) "xmlsafe:").size(), meta.end());
     540      keep_trying = true;
     541    }
    475542  }
    476543
     
    9371004      else fresh_metatext = metainfo.values[i];
    9381005
     1006      // New "truncate" special formatting option
     1007      if (meta.metacommand & mTruncate)
     1008      {
     1009    int truncate_length = meta.siblingoptions.getint();
     1010    text_t truncated_value = fresh_metatext;
     1011    if (truncated_value.size() > truncate_length)
     1012    {
     1013      truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
     1014    }
     1015    fresh_metatext = truncated_value;
     1016      }
     1017      // New "xmlsafe" special formatting option
     1018      if (meta.metacommand & mXMLSafe)
     1019      {
     1020    // Make it XML-safe
     1021    text_t text_xml_safe = "";
     1022    text_t::const_iterator text_iterator = fresh_metatext.begin();
     1023    while (text_iterator != fresh_metatext.end())
     1024    {
     1025      if (*text_iterator == '&') text_xml_safe += "&amp;";
     1026      else if (*text_iterator == '<') text_xml_safe += "&lt;";
     1027      else if (*text_iterator == '>') text_xml_safe += "&gt;";
     1028      else text_xml_safe.push_back(*text_iterator);
     1029      text_iterator++;
     1030    }
     1031    fresh_metatext = text_xml_safe;
     1032      }
     1033      // New "htmlsafe" special formatting option
     1034      if (meta.metacommand & mHTMLSafe)
     1035      {
     1036    // Make it HTML-safe
     1037    text_t text_html_safe = "";
     1038    text_t::const_iterator text_iterator = fresh_metatext.begin();
     1039    while (text_iterator != fresh_metatext.end())
     1040    {
     1041      if (*text_iterator == '&') text_html_safe += "&amp;";
     1042      else if (*text_iterator == '<') text_html_safe += "&lt;";
     1043      else if (*text_iterator == '>') text_html_safe += "&gt;";
     1044      else if (*text_iterator == '"') text_html_safe += "&quot;";
     1045      else text_html_safe.push_back(*text_iterator);
     1046      text_iterator++;
     1047    }
     1048    fresh_metatext = text_html_safe;
     1049      }
     1050
    9391051      if (metadata_spanwrap) {
    9401052    fresh_metatext = spanwrap_metatext(fresh_metatext,OID,meta.metaname,i);
     
    9611073    }
    9621074    else fresh_metatext = metainfo.values[position];
     1075
     1076    // New "truncate" special formatting option
     1077    if (meta.metacommand & mTruncate)
     1078    {
     1079      int truncate_length = meta.siblingoptions.getint();
     1080      text_t truncated_value = fresh_metatext;
     1081      if (truncated_value.size() > truncate_length)
     1082      {
     1083    truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
     1084      }
     1085      fresh_metatext = truncated_value;
     1086    }
     1087    // New "xmlsafe" special formatting option
     1088    if (meta.metacommand & mXMLSafe)
     1089    {
     1090      // Make it XML-safe
     1091      text_t text_xml_safe = "";
     1092      text_t::const_iterator text_iterator = fresh_metatext.begin();
     1093      while (text_iterator != fresh_metatext.end())
     1094      {
     1095    if (*text_iterator == '&') text_xml_safe += "&amp;";
     1096    else if (*text_iterator == '<') text_xml_safe += "&lt;";
     1097    else if (*text_iterator == '>') text_xml_safe += "&gt;";
     1098    else text_xml_safe.push_back(*text_iterator);
     1099    text_iterator++;
     1100      }
     1101      fresh_metatext = text_xml_safe;
     1102    }
     1103    // New "htmlsafe" special formatting option
     1104    if (meta.metacommand & mHTMLSafe)
     1105    {
     1106      // Make it HTML-safe
     1107      text_t text_html_safe = "";
     1108      text_t::const_iterator text_iterator = fresh_metatext.begin();
     1109      while (text_iterator != fresh_metatext.end())
     1110      {
     1111    if (*text_iterator == '&') text_html_safe += "&amp;";
     1112    else if (*text_iterator == '<') text_html_safe += "&lt;";
     1113    else if (*text_iterator == '>') text_html_safe += "&gt;";
     1114    else if (*text_iterator == '"') text_html_safe += "&quot;";
     1115    else text_html_safe.push_back(*text_iterator);
     1116    text_iterator++;
     1117      }
     1118      fresh_metatext = text_html_safe;
     1119    }
    9631120
    9641121    if (metadata_spanwrap) {
  • main/trunk/greenstone2/runtime-src/src/recpt/formattools.h

    r19302 r21752  
    4040        comCollection, comDocTermsFreqTotal};
    4141
    42 enum mcommand_t {mNone=0, mCgiSafe=1, mParent=2, mSibling=4, mChild=8, mSpecial=16};
     42enum mcommand_t {mNone=0, mCgiSafe=1, mParent=2, mSibling=4, mChild=8, mSpecial=16, mTruncate=32, mXMLSafe=64, mHTMLSafe=128};
    4343
    4444enum pcommand_t {pNone, pImmediate, pTop, pAll};
Note: See TracChangeset for help on using the changeset viewer.