remember the bracket notation thread?

Started by tomtoo, September 08, 2010, 08:24:13 PM

Previous topic - Next topic

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

#1
I have not tried it in newLISP:


[[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

#2
[(.*?)].*?[(.*)].*?[(.*)]

should work.



If you want to play with regex in newLISP, try Grepper - http://unbalanced-parentheses.nfshost.com/downloads/grepper.lsp.txt.src.html">//http://unbalanced-parentheses.nfshost.com/downloads/grepper.lsp.txt.src.html - surprisingly still working after a fashion! :)

tomtoo

#3
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.



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][][]



#!/usr/bin/newlisp

(change-dir (real-path))
(set
 'the-args (join (rest (rest (main-args))) " ")
 'a9 (read-file the-args)
'a (replace "nn" 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

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



#!/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

#5
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

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

tomtoo

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

cormullion

#8
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.