remember the bracket notation thread?

For the Compleat Fan
Locked
tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

remember the bracket notation thread?

Post by tomtoo »

Well, I'd like to revisit the editing-via-brackets thing. So, to begin with, may I please have a regular expression that matches three sets of brackets, empty or full?

[][][]

or

[stuff] [and] [junk]

or any combination imaginable.

Thank you. (stupid regex!)

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: remember the bracket notation thread?

Post by HPW »

I have not tried it in newLISP:

Code: Select all

\[[0-9a-zA-Z]*\]\[[0-9a-zA-Z]*\]\[[0-9a-zA-Z]*\]
Would match letters and numbers, but you can extend the character class.

PS: I used RegexBuddy
Hans-Peter

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: remember the bracket notation thread?

Post by cormullion »

Code: Select all

\[(.*?)\].*?\[(.*)\].*?\[(.*)\]
should work.

If you want to play with regex in newLISP, try Grepper - http://unbalanced-parentheses.nfshost.c ... t.src.html - surprisingly still working after a fashion! :)

tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: remember the bracket notation thread?

Post by tomtoo »

Hey cormullion, grepper slipped past me--looks good! Your example matched everything, though. Hans-Peter, thanks, yours did the trick. I added a character class for line-breaks and then got a script working. After I started using my script, though, I discovered that I also need one for punctuation, which I can't figure out. I also couldn't figure out how to match ] the number of times I need.

I have looked at regexbuddy before, but there were no real linux options but wine. I did find an emacs mode called regex-tool which I like, and which unlike the built in regexp-builder can do pcre.

anyway, I did an ugly work-around for the ] thing. My script, like me, does not have the advantage of great beauty, but it does work except for punctuation. I'm sure most of you could write it in four lines. As usual, shield your eyes. As for improvements, I would beg, but I'll resist the temptation for now. :-)

I wrote two versions, one outputs html and the other bbcode.

Code: Select all

now is the [tome][time][typo] for [ell][all][] good men two come to the aid of the quick brown fox that came to the aid of their party. [they passed the buck][now is the time for all good men][tf] and payed a buck for the movie of their choice.  Twin tweezers tweezed together, tickled [workers][toilers][alliteration is great] tweezing. [One][Four][] for the [asparagus][money][although I do like asparagus], two for the [doe][show][],three to get ready and four to go.[one][two][three]

[hellp][][]

Code: Select all

#!/usr/bin/newlisp

(change-dir (real-path))
(set 
 'the-args (join (rest (rest (main-args))) " ")
 'a9 (read-file the-args)
'a (replace "\n\n" a9 "\n</p><p>\n")
 'a2 (append the-args "-bracketed-nomarkup")
 'a3 (append the-args ".html")
 'aa ""
 'b (parse a {\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]} 0)
 'bowl '()
 'bowl3 '()
 'cup '()
 'bb (- (length bowl) 0)
 'nm -1
)

(write-file a2 a)

(set 'f (open a2 "r"))
(while (search f {\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]} true 0) 
(push $0 bowl -1))
(close f)

(push "" bowl -1)
(set 'bowl2 (copy bowl))

(define (x xx)(begin
  (unless (null? xx)
		  (set 'poop '())
		  (set 'zz (parse xx))
		  (replace {(\[{1})} (zz 0) {<span style="color:red;">} 0)
		  (replace {(\[{1})} (zz 1) {<span style="color:green;">} 0)
		  (replace {(\[{1})} (zz 2) {<span style="color:blue;">} 0)
		  (replace {(\]{1})} (zz 0) {</span>} 0)
		  (replace {(\]{1})} (zz 1) {</span>} 0)
		  (replace {(\]{1})} (zz 2) {</span>} 0)
		  (set 'zzz (string (zz 0)" "(zz 1)" "(zz 2)))
		  (push zzz bowl3 -1)
		  )
  ))
 

(map x bowl2)
(push "" bowl3 -1)

(dolist (item b)(push (append item (bowl3 (++ nm))) aa -1))
(write-file a3 aa)
(! (append "tidy -m " a3))
(exit)

tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: remember the bracket notation thread?

Post by tomtoo »

oh, here's the uglier one with the flubber.

Code: Select all

#!/usr/bin/newlisp
(change-dir (real-path))
(set 
 'the-args (join (rest (rest (main-args))) " ")
 'a (read-file the-args)
 ;'a1 (append the-args "-original")
 'a2 (append the-args "-bracketed-nomarkup")
 'a3 (append the-args "-markup")
 'aa ""
 'b (parse a {\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]} 0)
 'bowl '()
 'bowl3 '()
 'cup '()
 'bb (- (length bowl) 0)
 'nm -1
 'close-tag {[/color]}
)

(write-file a2 a)

(set 'f (open a2 "r"))
(while (search f {\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]\[[0-9a-zA-Z \n]*\]} true 0) 
(push $0 bowl -1))
(close f)

(push "" bowl -1)
(set 'bowl2 (copy bowl))

(define (x xx)(begin
  (unless (null? xx)
		  (set 'poop '())
		  (set 'zz (parse xx))
		  (replace {(\[{1})} (zz 0) {[color="red"]} 0)
		  (replace {(\[{1})} (zz 1) {[color="green"]} 0)
		  (replace {(\[{1})} (zz 2) {[color="blue"]} 0)
		  (replace {(\]{1})} (zz 0) {flubber} 0)
		  (set 'booger (parse (zz 0)"flubber"))
		  (set 'zz0 (string (booger 0)"]" (booger 1)(booger 2) {[/color]}))
		  (replace {(\]{1})} (zz 1) {flubber} 0) 
		  (set 'booger1 (parse (zz 1)"flubber"))
		  (set 'zz1 (string (booger1 0)"]" (booger1 1)(booger1 2) {[/color]}))
		  (replace {(\]{1})} (zz 2) {flubber} 0)
		  (set 'booger2 (parse (zz 2)"flubber"))
		  (set 'zz2 (string (booger2 0)"]" (booger2 1)(booger2 2) {[/color]}))
		  (push (append zz0 " " zz1 " " zz2) bowl3 -1)
		  )
  ))
 

(map x bowl2)
(push "" bowl3 -1)

(dolist (item b)(push (append item (bowl3 (++ nm))) aa -1))
(write-file a3 aa)
(exit)

tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: remember the bracket notation thread?

Post by tomtoo »

ok, I still would welcome improvements, but I just found an easy solution to the punctuation problem: put punctuation characters in the expression! Ta daaa, one problem solved.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: remember the bracket notation thread?

Post by cormullion »

I'm starting to remember now - was this thread the one you meant http://newlispfanclub.ryon.webfactional ... f=5&t=2516? It seems a long time ago now... :/

tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: remember the bracket notation thread?

Post by tomtoo »

That's the one. I only just now had a reason to try it.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: remember the bracket notation thread?

Post by cormullion »

You have an individual style. :)

At first glance - not sure that begin is needed. You might be able to work with string buffers rather than files. Also in HTML you can - these days - use SPAN tags around sections and control their visibility using CSS. For example, a mouseover could show you the original or replacement text. Then you could do the whole thing with one file.

Locked