🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.readers
12 messages
12 total messages Started by Ian Petts Mon, 09 May 2005 23:23
[slrn] Macro to delete block of text
#166514
Author: Ian Petts
Date: Mon, 09 May 2005 23:23
38 lines
2019 bytes
I've been using slrn for a couple of months now and think it's great.  I've
just started playing around with S-lang macros and am stuck on something I
thought would have been quite easy.

I'd like to be able to delete a block of text in the article body, defined by a
regular expression or the beginning/end of the article.

For example, given the text:

line 1
line 2
line 3
---
after1
after2
after3+end

I want to be able to delete from, say, '---' to the end of the article, but
have had little success. In particular, I can't seem to match from my
expression to the end of the text. Here is the code I have been using as a
test:

variable div = "---";
variable article="line 1\nline 2\nline 3\n---\nafter1\nafter2\nafter3+end";

if (string_match(article, div, 1)) {
    (article,) = strreplace(article, div, "", strlen(article));
    printf("%s\n", article);
}

This removes the divider just fine, but how can I include the rest of the text?
Adding a "\n.*" RE to the search string doesn't help.

Is there already a macro somewhere that does this? If not, can someone give me
some pointers?

Thanks,
Ian.
Re: [slrn] Macro to delete block of text
#166518
Author: W. Citoan
Date: Tue, 10 May 2005 01:41
38 lines
2113 bytes
Ian Petts wrote:
>  
>  I want to be able to delete from, say, '---' to the end of the
>  article, but have had little success. In particular, I can't seem to
>  match from my expression to the end of the text. Here is the code I
>  have been using as a test:
>  
>  variable div = "---";
>  variable article="line 1\nline 2\nline 3\n---\nafter1\nafter2\nafter3+end";
>  
>  if (string_match(article, div, 1)) {
>      (article,) = strreplace(article, div, "", strlen(article));
>      printf("%s\n", article);
>  }
>
>  This removes the divider just fine, but how can I include the rest of
>  the text?  Adding a "\n.*" RE to the search string doesn't help.

strreplace is the wrong function in this case.  It does not use regex so
you cannot give it a pattern.  Instead, you can use substr is pull out
the part that you want to keep.

   variable div = "---";
   variable article="line 1\nline 2\nline 3\n---\nafter1\nafter2\nafter3+end";

   variable start;

   if ( string_match(article, div, 1) ) {
      (start, ) = string_match_nth(0);
      article = substr(article, 1, start);
   }

   printf("%s\n", article);   

- W. Citoan
-- 
If voting changed anything, they'd make it illegal.
-- Graffiti
Re: [slrn] Macro to delete block of text
#166522
Author: Blinky the Shark
Date: Tue, 10 May 2005 03:33
20 lines
1614 bytes
W. Citoan wrote:

>    variable div = "---";
>    variable article="line 1\nline 2\nline 3\n---\nafter1\nafter2\nafter3+end";

>    variable start;

>    if ( string_match(article, div, 1) ) {
>       (start, ) = string_match_nth(0);
>       article = substr(article, 1, start);
>    }

>    printf("%s\n", article);   

I'm at a loss as to a reason for doing this.  Help me out, will you?

-- 
Blinky         Linux Registered User 297263
Killing all Usenet posts from Google Groups
Info:   http://blinkynet.net/comp/uip5.html
Re: [slrn] Macro to delete block of text
#166525
Author: Ian Petts
Date: Tue, 10 May 2005 04:23
16 lines
1547 bytes
On 2005-05-10, W. Citoan <wcitoan@NOSPAM-yahoo.com> wrote:
> strreplace is the wrong function in this case.  It does not use regex so
> you cannot give it a pattern.  Instead, you can use substr is pull out
> the part that you want to keep.
[...]
>       (start, ) = string_match_nth(0);

Aaah, that's the one. Thanks very much.

I'm now trying to work out how to count the number of lines in the
string, so I can delete from X to the end of the text.

I'll keep reading and post a new thread if I get stuck.

Regards,
Ian.
Re: [slrn] Macro to delete block of text
#166527
Author: Ian Petts
Date: Tue, 10 May 2005 04:35
14 lines
1449 bytes
On 2005-05-10, Blinky the Shark <no.spam@box.invalid> wrote:
> I'm at a loss as to a reason for doing this.  Help me out, will you?

I thought someone might ask  :-)

It's to remove advertising from posts. I was reading aus.jokes and got
sick of skipping over the "Joke is at the bottom! Subcribe to KenFuny"
text on each post.

Plus, I like playing around with scripts and it was a good excuse to
have a look at s-lang!

Regards,
Ian.
Re: [slrn] Macro to delete block of text
#166529
Author: Blinky the Shark
Date: Tue, 10 May 2005 04:56
25 lines
1869 bytes
Ian Petts wrote:

> On 2005-05-10, Blinky the Shark <no.spam@box.invalid> wrote:

>> I'm at a loss as to a reason for doing this.  Help me out, will you?

> I thought someone might ask  :-)

It was a trap!

> It's to remove advertising from posts. I was reading aus.jokes and got
> sick of skipping over the "Joke is at the bottom! Subcribe to KenFuny"
> text on each post.

How come that's on every post?

> Plus, I like playing around with scripts and it was a good excuse to
> have a look at s-lang!

Now you're talking.  :)

-- 
Blinky         Linux Registered User 297263
Killing all Usenet posts from Google Groups
Info:   http://blinkynet.net/comp/uip5.html
Re: [slrn] Macro to delete block of text
#166531
Author: Ian Petts
Date: Tue, 10 May 2005 05:08
9 lines
1543 bytes
On 2005-05-10, Blinky the Shark <no.spam@box.invalid> wrote:
>> It's to remove advertising from posts. I was reading aus.jokes and got
>> sick of skipping over the "Joke is at the bottom! Subcribe to KenFuny"
>> text on each post.
>
> How come that's on every post?

Well, not *every* post, but most of them. I think it's an automated post
from a joke site that's trying to drum up business for themselves.
Re: [slrn] Macro to delete block of text
#166535
Author: John E. Davis
Date: Tue, 10 May 2005 06:10
27 lines
1695 bytes
On 10 May 2005 14:13:27 +0950, Ian Petts <ipetts@gmail.com_no_spam_thanks>
wrote:
>I'm now trying to work out how to count the number of lines in the
>string, so I can delete from X to the end of the text.

If by the number of lines, you mean the number of \n characters, you
can use:

#v+
    lines = strchop (string, '\n', 0);
    num_lines = length (lines);
#v-

Then if you want just the first n lines, use:

#v+
    lines = lines[[0:n-1]];
#v-

Finally, to convert it back from an array of strings to a single
string, use strjoin:

#v+
    string = strjoin (lines, "\n");
#v-

--John
Re: [slrn] Macro to delete block of text
#166537
Author: Blinky the Shark
Date: Tue, 10 May 2005 06:30
17 lines
1962 bytes
Ian Petts wrote:
> On 2005-05-10, Blinky the Shark <no.spam@box.invalid> wrote:
>>> It's to remove advertising from posts. I was reading aus.jokes and got
>>> sick of skipping over the "Joke is at the bottom! Subcribe to KenFuny"
>>> text on each post.

>> How come that's on every post?

> Well, not *every* post, but most of them. I think it's an automated post
> from a joke site that's trying to drum up business for themselves.

Gotcha.  Yeah, I'd get tired of that, too.

-- 
Blinky         Linux Registered User 297263
Killing all Usenet posts from Google Groups
Info:   http://blinkynet.net/comp/uip5.html
Re: [slrn] Macro to delete block of text
#166560
Author: Ian Petts
Date: Tue, 10 May 2005 21:53
24 lines
1839 bytes
Ok, got it working now. Thanks for the help.

For anyone that's interested, here's the complete, working script:

define kill_KenFuny() {
    if ( current_newsgroup() == "aus.jokes" ) {
        variable div = "___________________________________\
_________________________";
        variable pos;
        variable len;
        variable article = article_as_string;

        if ( string_match(article, div, 1)) {
            (pos, ) = string_match_nth(0);
            pos += strlen(div);
            len = strlen(article);
            article = substr(article, pos, len - pos);
            replace_article(article);
        }
    }
}

define read_article_hook () { kill_KenFuny; }
Re: [slrn] Macro to delete block of text
#166564
Author: W. Citoan
Date: Tue, 10 May 2005 23:18
15 lines
1672 bytes
Ian Petts wrote:
>  Ok, got it working now. Thanks for the help.
>  
>  For anyone that's interested, here's the complete, working script:

I'm glad you got it working.  I am puzzled why you asked for something
that would keep everything before the delimiter and delete what was
after it when you actually wanted the reverse.  Oh well, I guess it was
a learning experience...

- W. Citoan
-- 
Any man's death diminishes me, because I am involved in Mankinde; And
therefore never send to know for whom the bell tolls; It tolls for thee.
-- John Donne
Re: [slrn] Macro to delete block of text
#166566
Author: Ian Petts
Date: Wed, 11 May 2005 01:51
20 lines
2084 bytes
On 2005-05-10, W. Citoan <wcitoan@NOSPAM-yahoo.com> wrote:
> I'm glad you got it working.  I am puzzled why you asked for something
> that would keep everything before the delimiter and delete what was
> after it when you actually wanted the reverse.  Oh well, I guess it
> was a learning experience...

I was initially thinking that I'd try a general all-purpose macro that I
could use to delete any block of text. So given different arguments, I
could delete from start to X, X to end, or X to Y.

This was harder than I was expecting (it's only my second s-lang script
after all - the first was just calling a couple of functions and binding
them to a key) so I decided to go with the less ambitious "get the thing
I wanted to do originally working and worry about the rest later."

Thanks again. I'll give enhanced version of the script a go next time
the scripting urge takes me  :-)

Regards,
Ian.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads