Forum: Ruby LyX chapter lister

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
slitt (Guest)
on 2005-12-10 00:51
(Received via mailing list)
Hi all,

The script under my sig is a filter that takes a LyX file from stdin and
spits
out the chapters. Right now it doesn't do parts, but it probably will
later
on. Perhaps some day it will handle the whole hierchy of
Part/chapter/section/subsection/subsubsection/paragraph/subparagraph.
Consider this version public domain with no warranty.

Steve L.
http://www.troubleshooters.com
removed_email_address@domain.invalid

#!/usr/bin/ruby

### SLURP STDIN
lines = []
while line = gets
	lines.push(line.chomp)
end

#### FIND CHAPTER NAMES
temp_lineno = -9999
chapno = 0
chapname = ""
in_chapter = false
lines.each do |line|
	if line =~ /^\s*\\layout Chapter/
		in_chapter = true
		chapname = ""
		chapno += 1
		#		puts "dia startchapter " + line + in_chapter.to_s
	elsif in_chapter and line =~  /^\s*\\layout/
		printf("%2d: %s\n", chapno, chapname) if chapno > 0
		#puts "dia endchapter " + line
		in_chapter = false
	elsif in_chapter
		chapname += (" " + line) unless line =~ /^\s*\\/ or line =~ /^\s*$/
	end
end
printf("%2d: %s\n", chapno, chapname) if chapname and chapname != ""
chneukirchen (Guest)
on 2005-12-10 21:04
(Received via mailing list)
Steve L. <removed_email_address@domain.invalid> writes:

> removed_email_address@domain.invalid
>
> #!/usr/bin/ruby
>
> ### SLURP STDIN
> lines = []
> while line = gets
> 	lines.push(line.chomp)
> end

lines = STDIN.readlines

> #### FIND CHAPTER NAMES
> temp_lineno = -9999
> chapno = 0
> chapname = ""
> in_chapter = false
> lines.each do |line|

# Alternatively, use ARGF.each { |line|

> 		chapname += (" " + line) unless line =~ /^\s*\\/ or line =~ /^\s*$/
elsif in_chapter && line =~ /^\s*(\\|$)/
  chapname << " " << line
end

> 	end
> end
> printf("%2d: %s\n", chapno, chapname) if chapname and chapname != ""

Also, I'd use puts and #{} instead of printf, but tastes vary.  Your
code would be a good example for the flip-flop operator (no religious
wars, please):

if (line =~ /^\s*\\layout Chapter/)..(line =~ /^\s*\\layout/)
  if line =~ /^\s*(\\|$)/
    chapname << " " << line
  end
end

Happy hacking,
slitt (Guest)
on 2005-12-10 23:10
(Received via mailing list)
On Saturday 10 December 2005 02:01 pm, Christian N. wrote:
> Steve L. <removed_email_address@domain.invalid> writes:

> >
> > #!/usr/bin/ruby
> >
> > ### SLURP STDIN
> > lines = []
> > while line = gets
> > 	lines.push(line.chomp)
> > end
>
> lines = STDIN.readlines

Thanks, I was looking for something like that.

Steve L.
http://www.troubleshooters.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.