Automatically Create <UL> Unordered List?

Discussion in 'Web Design and Development' started by Cooknn, Aug 20, 2007.

  1. Cooknn macrumors 68020

    Cooknn

    Joined:
    Aug 23, 2003
    Location:
    Fort Myers, FL
    #1
    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:
     
  2. bbarnhart macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    #2
    What language are you going to use on the server side? PHP, ASP.NET or something else?
     
  3. Cooknn thread starter macrumors 68020

    Cooknn

    Joined:
    Aug 23, 2003
    Location:
    Fort Myers, FL
    #3
    My site is based on PHP - built with MediaWiki.
     
  4. bbarnhart macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    #4
    Does PHP have any data structure classes... something like Map or Dequeue from the STL? This would make it easy.
     
  5. Cooknn thread starter macrumors 68020

    Cooknn

    Joined:
    Aug 23, 2003
    Location:
    Fort Myers, FL
    #5
    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:
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    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. 
    
     
  7. Cooknn thread starter macrumors 68020

    Cooknn

    Joined:
    Aug 23, 2003
    Location:
    Fort Myers, FL
    #7
    Wow. You ROCK, Todd. Thanks a bunch :D
     

Share This Page