[pan2: 42/68] Make separator user configurable.



commit 09e33d03de22b05cfb95d0461795924c9b6ea0e7
Author: K. Haley <haleykd users sf net>
Date:   Thu Aug 5 14:38:12 2010 -0600

    Make separator user configurable.
    
    The character used to replace ' ','_','.' can be set using
    the pref 'save-subj-seperator'.  The selection is limited to
    ' ', '_', '-' with '-' being the default.

 pan/gui/save-ui.cc                     |   10 ++++++----
 pan/usenet-utils/text-massager-test.cc |   31 ++++++++++++++++---------------
 pan/usenet-utils/text-massager.cc      |   19 ++++++++++++-------
 pan/usenet-utils/text-massager.h       |    2 +-
 4 files changed, 35 insertions(+), 27 deletions(-)
---
diff --git a/pan/gui/save-ui.cc b/pan/gui/save-ui.cc
index 3af574a..db4ab82 100644
--- a/pan/gui/save-ui.cc
+++ b/pan/gui/save-ui.cc
@@ -53,14 +53,14 @@ namespace
     return val;
   }
 
-  std::string expand_download_dir_subject (const char * dir, const char * subjectline)
+  std::string expand_download_dir_subject (const char * dir, const char * subjectline, const std::string &sep)
   {
     std::string val (dir);
-    std::string sub (subject_to_path(subjectline));
+    std::string sub (subject_to_path(subjectline, sep));
     std::string::size_type pos;
 
     while (((pos = val.find ("%s"))) != val.npos)
-      val.replace (pos, 2, sub.c_str(), sub.length());
+      val.replace (pos, 2, sub);
 
     return val;
   }
@@ -133,12 +133,14 @@ SaveDialog :: response_cb (GtkDialog * dialog,
     else if (s == "save-attachments-and-text") save_mode = TaskArticle::DECODE | TaskArticle::RAW;
     else                                       save_mode = TaskArticle::DECODE;
 
+    std::string sep( self->_prefs.get_string("save-subj-seperator", "-") );
+
     // make the tasks... 
     Queue::tasks_t tasks;
     foreach_const (std::vector<Article>, self->_articles, it)
     {
       if (subject_in_path)
-        path = expand_download_dir_subject(opath.c_str(), it->subject);
+        path = expand_download_dir_subject(opath.c_str(), it->subject, sep);
       tasks.push_back (new TaskArticle (self->_server_rank,
                                         self->_group_server,
                                         *it,
diff --git a/pan/usenet-utils/text-massager-test.cc b/pan/usenet-utils/text-massager-test.cc
index 3e142a5..62dbffd 100644
--- a/pan/usenet-utils/text-massager-test.cc
+++ b/pan/usenet-utils/text-massager-test.cc
@@ -32,6 +32,7 @@ int main (void)
    in = "> a\n> b\n> c";
    out = tm.fill (in);
    expected_out = "> a\n> b\n> c";
+   std::cout<<out<<"\n---\n"<<expected_out<<std::endl;
    check (out == expected_out);
 
    /* wrap real-world 1 */
@@ -261,75 +262,75 @@ int main (void)
    out = tm.mute_quotes (in);
    check (out == expected_out);
 
-   const char *in2;
+   const char *in2, *sep="_";
    in2 = "prefix - one ...__   - two - three";
    expected_out = "prefix_-_one_-_two_-_three";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix File 25 of 1000 one Post 1 _ 25: two file 1_10 end";
    expected_out = "prefix_one_two_end";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix [1 of 10] middle (2 / 20) end";
    expected_out = "prefix_middle_end";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix \" file name here\" yEnc ending";
    expected_out = "prefix";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix file name here yEnc ending";
    expected_out = "prefix_file_name";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix \"stuff \"\" file name here.sdf\" bar baz.gd ending";
    expected_out = "prefix_stuff_bar_ending";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix 2Kb one 3kB two 10 KB three 100 Bytes four [5 KB] end \t";
    expected_out = "prefix_one_two_three_four_end";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "prefix / a \\ b < c > d | e * f ? g ' h \" end";
    expected_out = "prefix_a_b_c_d_e_f_g_h_end";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "[foo]     K's    \"kpsh eg02b.jpg\" (0/2) 685k bar ";
    expected_out = "[foo]_K_s_bar";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "[ASDF-FDSE]  Name1 & Name2 - Spettertje - 01 title here  (thx AntA)  Post 6_6 - File 9_9 - aaspettertje01.sfv (1/1)";
    expected_out = "[ASDF-FDSE]_Name1_&_Name2_-_Spettertje_-_01_title_here_(thx_AntA)";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "Evil, Wicked Queen-e01.jpg(1/01)";
    expected_out = "Evil,_Wicked";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "some text here...  and more... 123.jpg";
    expected_out = "some_text_here_and_more";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "who are you? 123.jpg";
    expected_out = "who_are_you";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
    in2 = "one - two three [1/2] - \"00 - title spaces.foo\" yEnc (1/5)";
    expected_out = "one_-_two_three";
-   out = pan::subject_to_path(in2);
+   out = pan::subject_to_path(in2, sep);
    //std::cout<<"input: '"<<in2<<"'\noutput: '"<<out<<"'\n"<<std::endl;
    check(out == expected_out);
 
diff --git a/pan/usenet-utils/text-massager.cc b/pan/usenet-utils/text-massager.cc
index e51fa9c..30203a7 100644
--- a/pan/usenet-utils/text-massager.cc
+++ b/pan/usenet-utils/text-massager.cc
@@ -361,19 +361,24 @@ TextMassager :: rot13_inplace (char * text)
 }
 
 std::string
-pan :: subject_to_path (const char * subjectline)
+pan :: subject_to_path (const char * subjectline, const std::string &seperator)
 {
   gchar *str1, *str2;
+  const char *sep;
   std::string val (subjectline);
   std::string::size_type pos;
   //stupid hack to silence the compiler
   GRegexCompileFlags cf0((GRegexCompileFlags)0);
   GRegexMatchFlags mf0((GRegexMatchFlags)0);
 
-  // delete everything after the last hyphen
-  // (perhaps if >=3 hyphens, delete everything after the 2nd hyphen?)
-  //if ((pos = val.rfind("-")) != val.npos)
-  //  val.erase(pos);
+  if (seperator.length() != 1)
+    sep = "-";
+  else switch (seperator[0]) {
+    case ' ':
+    case '-':
+    case '_': sep = seperator.c_str(); break;
+    default : sep = "-"; break;
+  }
 
   // strip out newspost/Xnews-style multi-part strings
   GRegex *mp1 =g_regex_new("\\s*(?:[Ff]ile|[Pp]ost) [0-9]+ *(?:of|_) *[0-9]+[: ]?", cf0, mf0, NULL);
@@ -407,9 +412,9 @@ pan :: subject_to_path (const char * subjectline)
   g_free(str2);
   g_regex_unref(badc);
 
-  // remove any extraneous whitespace / underscores
+  // remove any extraneous whitespace, '_', and '.'
   GRegex *ext =g_regex_new("[\\s_\\.]+", cf0, mf0, NULL);
-  str2 = g_regex_replace_literal(ext, str1, -1, 0, "_", mf0, NULL);
+  str2 = g_regex_replace_literal(ext, str1, -1, 0, sep, mf0, NULL);
   g_free(str1);
   g_regex_unref(ext);
 
diff --git a/pan/usenet-utils/text-massager.h b/pan/usenet-utils/text-massager.h
index a2c48d5..47e4977 100644
--- a/pan/usenet-utils/text-massager.h
+++ b/pan/usenet-utils/text-massager.h
@@ -60,7 +60,7 @@ namespace pan
    *
    * @ingroup usenet_utils
    */
-  std::string subject_to_path (const char * subjectline);
+   std::string subject_to_path (const char * subjectline, const std::string &seperator);
 
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]