simple template replacement

Started by tomtoo, December 02, 2009, 08:06:38 AM

Previous topic - Next topic

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.

:-)

cormullion

#1
Hmm a bit too vague there...! You'd probably need to supply sample source data and stuff. But some thoughts:


(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.


(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,

tomtoo

#2
Quote from: "cormullion"Hmm a bit too vague there]


sorry about that.



,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!

cormullion

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


(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)

tomtoo

#4
Hi guys,



this thing I have more or less working only correctly makes ten replacements.  can you see what's wrong?



the newlisp

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

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

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


cormullion

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

xytroxon

#6
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
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

tomtoo

#7
Quote from: "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... ?


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.

cormullion

#8
Perhaps check those regex/find things. Perhaps "2=" is found twice, once in 2= once in 12=...?



I like wild code... :0

xytroxon

#9
Quote from: "cormullion"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
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

Lutz

#10
QuoteP.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/development/latest/newlisp_manual.html#transpose">http://www.newlisp.org/downloads/develo ... #transpose">http://www.newlisp.org/downloads/development/latest/newlisp_manual.html#transpose

tomtoo

#11
Thanks guys, that did the trick.  originally, the tags were ",1," and I changed them to "1=".  I changed them back. :-)

cormullion

#12
Quote from: "xytroxon"Scroll back up three posts ;p)


oops - didn't see that! Need a bigger monitor! :)