summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2023-03-05 03:06:47 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2023-03-05 03:06:47 +0900
commit9ffc02f8e7229702277c501c1ac8310e8e451dbc (patch)
tree8dc5401aed7bf77d485329c658b1c5976ce7ba83
parentaf8da8383d13e22b042ea90db48841ad39f43e96 (diff)
downloadhybring-9ffc02f8e7229702277c501c1ac8310e8e451dbc.tar.gz
Add anchors to headings
-rw-r--r--src/http.cr10
-rw-r--r--src/xhtml.cr17
2 files changed, 18 insertions, 9 deletions
diff --git a/src/http.cr b/src/http.cr
index 1d0be13..bfd7bf7 100644
--- a/src/http.cr
+++ b/src/http.cr
@@ -23,6 +23,7 @@ require "uri"
 require "./sqlite"
 require "./xhtml"
 
+HTML_HEADINGS = Set{"applicants", "criteria", "joining", "members"}
 MAX_CONTENT_LENGTH = 4096
 MAX_NICK_LENGTH = 32
 OPENNIC_TLD = Set{".bbs", ".chan", ".cyb", ".dyn", ".epic",
@@ -38,11 +39,9 @@ class Server
     @db = Database.new cfg.db, cfg.opennic_remote, cfg.icann_remote
 
     @opennic_host = URI.parse(cfg.opennic_remote).host
-    @opennic_page = Page.new cfg.opennic_local, cfg.opennic_remote,
-                             cfg.api, @db
+    @opennic_page = Page.new cfg.opennic_local, cfg.opennic_remote, cfg.api, @db
     @opennic_page.write
-    @icann_page = Page.new cfg.icann_local, cfg.icann_remote,
-                           cfg.api, @db
+    @icann_page = Page.new cfg.icann_local, cfg.icann_remote, cfg.api, @db
     @icann_page.write
 
     @server = HTTP::Server.new do |context|
@@ -100,6 +99,9 @@ class Server
         errors["opennic"] = "Must be unique" if opennic == params["opennic"]
         errors["icann"] = "Must be unique" if icann == params["icann"]
       end
+      if HTML_HEADINGS.includes? params["nick"]
+        errors["nick"] = "Reserved names: #{HTML_HEADINGS.join ", "}"
+      end
 
       if errors.empty?
         @db.add_applicant params["nick"], params["opennic"], params["icann"]
diff --git a/src/xhtml.cr b/src/xhtml.cr
index c62b23a..35f9d62 100644
--- a/src/xhtml.cr
+++ b/src/xhtml.cr
@@ -48,8 +48,15 @@ class Page
     @db = db
   end
 
+  def heading(xml, level, text)
+    id = text.gsub ' ', '-'
+    xml.element "h#{level}", id: id do
+      xml.element "a", href: "##{id}" do xml.text text end
+    end
+  end
+
   def criteria(xml)
-    xml.element "h2" do xml.text "criteria" end
+    heading xml, 2, "criteria"
     xml.element "p" do
       xml.text "We accept pretty much any site"
       xml.text " served under an OpenNIC domain"
@@ -115,7 +122,7 @@ class Page
   end
 
   def member(xml, nick, opennic, icann)
-    xml.element "h3" do xml.text nick end
+    heading xml, 3, nick
     xml.text "OpenNIC: "
     xml.element "a", href: opennic do xml.text opennic end
     xml.element "br"
@@ -143,12 +150,12 @@ class Page
           end
           criteria xml
 
-          xml.element "h2" do xml.text "members" end
+          heading xml, 2, "members"
           @db.members.each_value do |nick, opennic, icann|
             member xml, nick, opennic, icann
           end
 
-          xml.element "h2" do xml.text "joining" end
+          heading xml, 2, "joining"
           xml.element "p" do
             xml.text "First, add "
             xml.element "a",
@@ -175,7 +182,7 @@ class Page
           end
           form xml, errors, params
 
-          xml.element "h2" do xml.text "applicants" end
+          heading xml, 2, "applicants"
           @db.applicants.each_value do |nick, opennic, icann|
             member xml, nick, opennic, icann
           end