Automatically Create <UL> Unordered List?

Cooknn

macrumors 68020
Original poster
Aug 23, 2003
2,111
0
Fort Myers, FL
I have a huge outline that I need to get into an unordered list. There several nested levels which make my head spin when I try to do it manually. If I ever figure this out, I'm going to use the code here to make a clickable explorer tree. The data is from my genealogy website. If someone could point me in the right direction I would be very thankful :eek:
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
What language are you going to use on the server side? PHP, ASP.NET or something else?
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
Does PHP have any data structure classes... something like Map or Dequeue from the STL? This would make it easy.
 

Cooknn

macrumors 68020
Original poster
Aug 23, 2003
2,111
0
Fort Myers, FL
Does PHP have any data structure classes... something like Map or Dequeue from the STL? This would make it easy.
I know just enough about PHP to be dangerous. I really don't know whether or not it contains anything like what you mentioned :eek:
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
This will do it. It's Ruby. Copy and paste it into a plain text file. Call it, for example, ulist.rb and save it on your desktop.

Then, in Terminal, navigate to your /Desktop and enter:

ruby ulist.rb

Copy the output to whereever it needs to go. It will read the file right off the web.

This took me about 15 minutes to write. It's not super efficient, but it gets the job done, and, it's readable. :)

Todd

Code:
# ruby script to make an unordered list from indented data. 

require 'open-uri' ; 
data = nil ; 
open("http://listing360.com/temp/outline.txt") {|f| data = f.readlines } ; 

indent = 0 ;  # current indent level 

data.each {|d| 
	# split the line at the first blank, and count how many dots there are in the left side. 
	left , right = d.split(/ /,2) ;   # isolate the "1.2.3.4." part of the line 
	
	# count dots, indent or un-indent as needed.  
	dots = 0 ; 
	left.split(//).each {|char|   # count periods. 
		dots += 1 if char=='.' } ; 

	if dots < indent then 
		(indent-dots).times do ; puts "</ul>" ; end ; # terminate a list 
	elsif dots > indent then  
		(dots-indent).times do ; puts "<ul>" ; end ;  # start a new list 
	end ; 
	puts "<li>#{right.chomp}</li>" ;  # add an item to the list. chomp=Remove CRLF from the end.
	indent = dots ; 
} 

indent.times do ; puts "</ul>" ;  end ; # do any final termination.