# LyX chapter lister

**URL:** <https://www.ruby-forum.com/t/lyx-chapter-lister/52999>\
**Category:** Ruby\
**Created:** [December 9, 2005, 11:51pm UTC](https://www.ruby-forum.com/t/lyx-chapter-lister/52999 "2005-12-09T23:51:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@slitt](https://www.ruby-forum.com/u/slitt)\
**Post date:** [December 9, 2005, 11:51pm UTC](https://www.ruby-forum.com/t/lyx-chapter-lister/52999/1 "2005-12-09T23:51:40Z")

</div>

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.

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

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 != “”

---

<div class="post-metadata">

**Author:** ![slitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@slitt](https://www.ruby-forum.com/u/slitt)\
**Post date:** [December 10, 2005, 8:04pm UTC](https://www.ruby-forum.com/t/lyx-chapter-lister/52999/2 "2005-12-10T20:04:44Z")

</div>

Steve L. [removed\_email\_address@domain.invalid](mailto: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,

---

<div class="post-metadata">

**Author:** ![slitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@slitt](https://www.ruby-forum.com/u/slitt)\
**Post date:** [December 10, 2005, 10:10pm UTC](https://www.ruby-forum.com/t/lyx-chapter-lister/52999/3 "2005-12-10T22:10:49Z")

</div>

On Saturday 10 December 2005 02:01 pm, Christian N. wrote:

> Steve L. [removed\_email\_address@domain.invalid](mailto: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.

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

removed\_email\_address@domain.invalid
