Page 1 of 1
					
				simple template replacement
				Posted: Wed Dec 02, 2009 4:06 pm
				by tomtoo
				Howdy guys,
I want to label a text file with something like, ",1," ",2," etc, and
copy what follows the 1 to what precedes the 2, etc, then stick it into
corresponding spot in a template.
There are regular expressions and junk in there I'm not too
comfortable with, so I thought I'd rather ask than struggle.
:-)
			 
			
					
				Re: simple template replacement
				Posted: Wed Dec 02, 2009 5:19 pm
				by cormullion
				Hmm a bit too vague there...! You'd probably need to supply sample source data and stuff. But some thoughts:
Code: Select all
(set 'text {foo foo ,1, bar bar ,2, baz baz ,3,})
(find-all {(.*?),1,} text)
Here $1 now contains the first match of the parenthesized term - foo foo.
To move stuff around, you can try rebuilding the target string from the components you  found.
Code: Select all
(set 'text {foo foo ,1, bar bar ,2, baz baz ,3,})
(replace {(.*?),1,(.*?),2,(.*?)} text (string $3 $2 $1) 0)
(println text)
;->  bar bar foo foo  baz baz ,3,
 
			 
			
					
				Re: simple template replacement
				Posted: Wed Dec 02, 2009 7:15 pm
				by tomtoo
				cormullion wrote:Hmm a bit too vague there]
sorry about that.
Code: Select all
,1,
Heading
,2,
paragraph paragraph paragraph paragraph paragraph 
paragraph paragraph paragraph paragraph paragraph 
paragraph paragraph paragraph paragraph paragraph 
,3,
listitem
,4,
listitem
,5,
nested listitem
,6,
paragraph paragraph paragraph paragraph paragraph 
paragraph paragraph paragraph paragraph paragraph 
Is that better?
I'd like to write a plain old text file and not have to worry about the mark-up, or worse, some mark-up helper that's just as bad.  This seems simple to me.  maybe better than the comma-digit-comma thing would just be the digit on a line by itself.
The output I have in mind are pdfs or whatever via latex.
Thanks!
 
			 
			
					
				Re: simple template replacement
				Posted: Wed Dec 02, 2009 10:10 pm
				by cormullion
				I'm not convinced I understand what you mean - unless you just want to sort the different sections of the file according to the number that 'precedes' them...? (because "copy what follows the 1 to what precedes the 2," doesn't seem to do much...).
If that's what you want, though, here's a script that sorts a file based on what you define as a section divider:
Code: Select all
(set 'l (parse (read-file ((main-args) 2)) "\n" 0)) ; or whatever reads the file
(set 'temp-store '() 'section '())
(define (new-section? lst lmnt)
   (if (starts-with (lst lmnt) ",\\d," 0))) ;-< section starts with ,1, ,2, etc.
(for (i 0 (- (length l) 1))
   (when (new-section? l i)
       (push section temp-store -1)
       (set 'section '()))
   (push (l i) section -1))
(push section temp-store -1)
(sort temp-store)
(print (join (flat (clean empty? temp-store)) "\n"))
(exit)
 
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 1:22 pm
				by tomtoo
				Hi guys,
this thing I have more or less working only correctly makes ten replacements.  can you see what's wrong?
the newlisp
Code: Select all
#!/usr/bin/newlisp
(set
   'written
 '((1 "1=" "one")
   (2 "2=" "two")
   (3 "3=" "three")
   (4 "4=" "four")
   (5 "5=" "five")
   (6 "6=" "six")
   (7 "7=" "seven")
   (8 "8=" "eight")
   (9 "9=" "nine")
   (10 "10=" "ten")
   (11 "11=" "eleven")
   (12 "12=" "twelve")
   (13 "13=" "thirteen")
   (14 "14=" "fourteen")
   (15 "15=" "fifteen")
   (16 "16=" "sixteen")
   (17 "17=" "seventeen")
   (18 "18=" "eighteen")
   (19 "19=" "nineteen")
   (20 "20=" "twenty")
   (21 "21=" "twenty-one")
   (22 "22=" "twenty-two")
   (23 "23=" "twenty-three")
   (24 "24=" "twenty-four")
   (25 "25=" "twenty-five"))
 'writ (transpose written)
 'd (push "" (writ 0))
 'e (push "" (writ 1))
 'w (push "" (writ 2))
 'n 0
 'box '()
 'ee (join (rest (rest (main-args))) " ")
 'f (read-file ee)
  'segs (parse f "\\d+=" 0)
 'mm (-(length segs) 1)
 )
(for (c 1 mm)(begin
               (push (cons (w (set 'zz (inc n 1))) (trim (segs (copy zz))"\n")) box -1)
               ))
(define (c cc)
  (set (sym (cc 0)) (cons (cc 1) '())))
(map c box)
(set 'a (read-file "bb.html"))
(dolist (item e)
		(if (find item a)(replace item a (first(eval-string(w (find item e))))0)
		 ))
(set 'ooo (parse ee "."))
(set 'oooo (append (ooo 0) ".html"))
(write-file oooo a)
(exit)
the source file
Code: Select all
1=
one text green text green text green text green text green text 
2=
two blue text blue text blue text blue text blue text blue text blue text 
3=
three brown text brown text brown text brown text brown text brown text brown text 
4=
four green text green text green text green text green text green text 
5=
five blue text blue text blue text blue text blue text blue text blue text 
6=
six brown text brown text brown text brown text brown text brown text brown text 
7=
seven green text green text green text green text green text green text 
8=
eight blue text blue text blue text blue text blue text blue text blue text 
9=
nine brown text brown text brown text brown text brown text brown text brown text 
10=
 ten green text green text green text green text green text green text 
11=
eleven blue text blue text blue text blue text blue text blue text blue text 
12=
twelve brown text brown text brown text brown text brown text brown text brown text 
the template
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<style type="text/css">
.green {
border: 6px solid green;
margin: 10%;
padding:4px;
color: green;
}
.blue {
border: 6px solid blue;
margin: 10%;
padding:4px;
color:blue;
}
.brown {
border: 6px solid brown;
margin: 10%;
padding:4px;
color:brown;
}
</style>
<head>
<meta name="generator" content=
"HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
<title></title>
</head>
<body>
<div class="green">
1=
</div>
<div class="blue">
2=
</div>
<div class="brown">
3=
</div>
<div class="green">
4=
</div>
<div class="blue">
5=
</div>
<div class="brown">
6=
</div>
<div class="green">
7=
</div>
<div class="blue">
8=
</div>
<div class="brown">
9=
</div>
<div class="green">
10=
</div>
<div class="blue">
11=
</div>
<div class="brown">
12=
</div>
</body>
</html>
 
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 4:48 pm
				by cormullion
				hmm, some wild code there. 
Edit: I think I see what you're doing now. It's doing 12 for me. Because the  length is 12... ?
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 7:41 pm
				by xytroxon
				The replace loop replaced 1= and 2= before  it does 11= and 12=
It sees the embedded 1= in 11= and 2= in 12= and replaces it after it does the original 1= and 2=
Use a more unique looking tag, something like =1= =2= ... =11= =12= etc...
P.S. Interesting use for transpose I had never thought of... Lutz, should this be documented? Or is there a better function to use for a string matrix? Or should a flag be added to transpose to prevent floating point conversion in these cases?
-- xytroxon
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 7:44 pm
				by tomtoo
				cormullion wrote:hmm, some wild code there. 
Edit: I think I see what you're doing now. It's doing 12 for me. Because the  length is 12... ?
yeah, pretty wild.  I often hesitate to "show off" just because it's so dang ugly...
I get 12 replacements but only 10 correct replacements.  For 11 and up it replaces from somewhere in the first ten.
 
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 8:00 pm
				by cormullion
				Perhaps check those regex/find things. Perhaps "2=" is found twice, once in 2= once in 12=...?
I like wild code... :0
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 8:04 pm
				by xytroxon
				cormullion wrote:Perhaps check those regex/find things. Perhaps "2=" is found twice, once in 2= once in 12=...?
I like wild code... :0
Scroll back up three posts ;p)
-- xytroxon
 
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 9:07 pm
				by Lutz
				P.S. Interesting use for transpose I had never thought of... Lutz, should this be documented? Or is there a better function to use for a string matrix? Or should a flag be added to transpose to prevent floating point conversion in these cases?
Since version 7.4.8 'transpose' handles any data type and it only re-arranges data. There is no transformation from integers to floats anymore when transposing matrices containing numbers. The documentation fixes this for 10.2:
http://www.newlisp.org/downloads/develo ... #transpose 
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 9:13 pm
				by tomtoo
				Thanks guys, that did the trick.  originally, the tags were ",1," and I changed them to "1=".  I changed them back. :-)
			 
			
					
				Re: simple template replacement
				Posted: Sat Mar 13, 2010 9:30 pm
				by cormullion
				xytroxon wrote:Scroll back up three posts ;p)
oops - didn't see that! Need a bigger monitor! :)