linkgen.rb (891B)
1 #!/usr/bin/env ruby 2 # frozen_string_literal: true 3 4 # Takes links YAML file on stdin, outputs HTML <ul> on stdout 5 require 'yaml' 6 7 begin 8 input = $stdin.read 9 if input.empty? 10 warn 'No input provided.' 11 exit 1 12 end 13 categories = YAML.safe_load(input) 14 rescue StandardError 15 warn "Couldn't parse input." 16 exit 1 17 end 18 19 outlines = "<ul id='links'>\n" 20 categories.each_with_index do |(cat, links), i| 21 outlines += <<~HTML 22 <li> 23 <a tabindex="#{i + 1}"><img src="img/#{cat.downcase}.svg"></a> 24 <ul> 25 HTML 26 links.each do |link| 27 outlines += if link.keys.first == '-' 28 " <li class=\"separator\">#{link.values.first}</li>\n" 29 else 30 " <li><a href=\"#{link.values.first}\">#{link.keys.first}</a></li>\n" 31 end 32 end 33 outlines += <<~HTML 34 </ul> 35 </li> 36 HTML 37 end 38 outlines += '</ul>' 39 40 puts outlines