[mini] Generate HTML alike UL list from array

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
krzysiunet
Posts: 6
Joined: 18 Jul 2017, 16:09
Location: Katowice, Poland
Contact:

[mini] Generate HTML alike UL list from array

20 Oct 2017, 17:51

Hey! Very simple code, very simple task. It's public domain or if it's a problem in your country, then CC-0

Description
Creates HTML alike list (with new lines, bullets, indentation and whatnot) from array. I'm using mostly it for MsgBox when there's need of listing things, i.e. something failed because
  • . Of course you can do it manually, but I think it keeps code cleaner. This way also ensures the lists will look the same, which is just nice for users.

    Parameters
    • (array) items - array of items
    • (int) indent - indentation level (default: 1; 0 for no indentation)
    • (str) liSymbol - list symbol, i.e. prefix of item (default: "•")

    Result
    String with ready to use list.

    Note 1: the indentation is two spaces. I didn't use tabulation character, as it's often too big.
    Note 2: the list starts with first item, ends with new line character. For [whole] list suffix and prefix you'd need to provide following elements: [Prefix][New line char]
    • [Suffix]. It's inconsistent with HTML (HTML <ul> doesn't need \n before list) because cases when you want to give just list, without prefix, so new line character would be wrong to use. To make it add new character before each list, add "out := "`t"" as a first line of body function.

      Code

      Code: Select all

      makeList(items, indent = 1, liSymbol = "•")
      {
      	indStr :=
      	
      	if indent > 0
      		Loop, %indent%
      			indStr .= "  "
      	
      	for k,item in items
      		out .= indStr liSymbol " " item "`n"
      		
      	return out
      }
      
      Example
      Code

      Code: Select all

      MsgBox % makeList(["Foo", "Bar", "Foobar"])
      Result

      Code: Select all

        • Foo
        • Bar
        • Foobar
      
      Code

      Code: Select all

      MsgBox % "Update failed. Possible causes: `n" makeList(["Solar flares", "Server is down", "You broke it"],5,"-") "Please try again later."
      Result

      Code: Select all

      Update failed. Possible causes: 
                - Solar flares
                - Server is down
                - You broke it
      Please try again later.
      
Do you think my work is worth donating? I'm trying to keep my tools FLOSS, but donation or your feedback motivates me to do even more! https://www.paypal.com/paypalme/krzysiunet/5EUR
My works, tutorials etc: Krzysiu's random things * Krzysiu Photography * Mini webapps (including icon generator for AHK) * Comic strips (in Polish) * YT channel (both EN and PL) * Public domain cliparts

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: RussF and 97 guests