summary refs log tree commit diff homepage
path: root/src/xhtml.cr
blob: b9d6d4c77cc91adde30005bb0f31ba7337949985 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# XHTML generation
# Copyright (C) 2023  Nguyễn Gia Phong
#
# This file if part of hybring.
#
# Hybring is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hybring is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with hybring.  If not, see <https://www.gnu.org/licenses/>.

require "uri"
require "xml"

require "./http"
require "./sqlite"

class Page
  def initialize(static_dir : Path, static_url : String, api_url : String,
                 css : Path, db : Database)
    Dir.mkdir_p static_dir
    File.copy css, static_dir / "style.css"
    @static_file = static_dir / "index.xhtml"
    @static_url = static_url
    @static_host = URI.parse(static_url).host
    @api_url = api_url
    @db = db
  end

  def heading(xml, level, text)
    xml.element "h#{level}", id: text do
      xml.element "a", href: "##{text}" do xml.text text end
    end
  end

  def criteria(xml)
    heading xml, 2, "criteria"
    xml.element "p" do
      xml.text "We accept pretty much any site"
      xml.text " served under an OpenNIC domain"
      xml.text " meeting the following requirements."
    end
    xml.element "ul" do
      xml.element "li" do xml.text "Family-friendly content" end
      xml.element "li" do xml.text "No violence incitation or doxing" end
      xml.element "li" do xml.text "No user tracking" end
      xml.element "li" do
        xml.text "ICANN counterpart accessible via HTTPS"
      end
      xml.element "li" do
        xml.text "Both linking to neighboring members"
      end
    end
    xml.element "p" do
      xml.text "Note that the two sites needn't be the same, as long as"
      xml.text " they represent the same entity, e.g. person or organization."
      xml.text "  The clear net site is for serving visitors"
      xml.text " without OpenNIC access or preferring secure connections."
    end
  end

  def input(xml, type, name, pattern, label, error, value)
    xml.element "label", for: name do xml.text label end
    xml.element "input", type: type, name: name, pattern: pattern,
                value: value, required: "required"
    xml.element "br"
    if error
      xml.element "label", for: name, class: "error" do xml.text "Error:" end
      xml.element "label", for: name, class: "error" do xml.text error end
      xml.element "br"
    end
  end

  def form(xml, errors = {} of String => String,
           params = {} of String => String)
    xml.element "p" do xml.text "Then, please fill out the form below." end
    nick = params.fetch("nick", nil)
    xml.element "form", action: @api_url, method: "POST" do
      input xml, "text", "nick", NICK_PATTERN, "Nickname:",
            errors.fetch("nick", nil), nick || ""
      input xml, "url", "opennic", "https?://.*", "OpenNIC URL:",
            errors.fetch("opennic", nil), params.fetch("opennic", "")
      input xml, "url", "icann", "https://.*", "ICANN URL:",
            errors.fetch("icann", nil), params.fetch("icann", "")
      xml.element "span" do
        xml.element "input", type: "hidden", name: "host", value: @static_host
      end
      xml.element "input", type: "submit", value: "Let me in!"
      xml.element "br"
    end

    xml.element "p" do
      xml.text "Your application is pending for approval."
      xml.text "  Please subscribe to this "
      xml.element "a", href: "#{@static_url}/#{nick}.atom" do
          xml.text "web feed"
      end
      xml.text " for further instructions."
    end if nick && errors.empty?
  end

  def member(xml, nick, opennic, icann)
    heading xml, 3, nick
    xml.text "OpenNIC: "
    xml.element "a", href: opennic do xml.text opennic end
    xml.element "br"
    xml.text "ICANN: "
    xml.element "a", href: icann do xml.text icann end
    xml.element "br"
  end

  def build(errors = {} of String => String,
            params = {} of String => String) : String
    XML.build encoding: "UTF-8", indent: "  " do |xml|
      xml.element "html", xmlns: "http://www.w3.org/1999/xhtml", lang: "en" do
        xml.element "head" do
          xml.element "meta", name: "viewport",
                      content: "width=device-width,initial-scale=1.0"
          xml.element "meta", name: "color-scheme", content: "light dark"
          xml.element "base", href: @static_url
          xml.element "link", rel: "icon", href: "data:,"
          xml.element "link", rel: "stylesheet", href: "style.css"
          xml.element "title" do xml.text "le cercle libre" end
        end
        xml.element "body" do
          xml.element "h1" do xml.text "le cercle libre" end
          xml.element "p" do
            xml.text "The Free Circle is a web ring for sites"
            xml.text " with an OpenNIC domain."
          end
          criteria xml

          heading xml, 2, "members"
          @db.exec "SELECT nick, opennic, icann
                    FROM member
                    WHERE official = 1" do |row|
            member xml, row[0].text, row[1].text, row[2].text
          end

          heading xml, 2, "joining"
          xml.element "p" do
            xml.text "First, add "
            xml.element "a",
                        href: "https://html.spec.whatwg.org/#the-a-element" do
                xml.text "anchors"
            end
            xml.text " refering to the following neighbors to your sites"
            xml.text " on both OpenNIC and ICANN domains."
            xml.text "  Your neighbors will change as people come and go,"
            xml.text " so you will be given an Atom feed with instructions"
            xml.text " to keep the hyperlinks up to date."
          end
          xml.element "ul" do
            @db.exec "SELECT neighbor.opennic, member.opennic
                      FROM member INNER JOIN member AS neighbor
                      ON member.left = neighbor.nick
                      WHERE member.official = 1
                      ORDER BY member.rowid ASC LIMIT 1" do |row|
              left, right = row[0].text, row[1].text
              xml.element "li" do
                xml.text "Left: "
                xml.element "a", href: left do xml.text left end
              end
              xml.element "li" do
                xml.text "Right: "
                xml.element "a", href: right do xml.text right end
              end
            end
          end
          form xml, errors, params

          heading xml, 2, "applicants"
          @db.exec "SELECT nick, opennic, icann
                    FROM member
                    WHERE official = 0" do |row|
            member xml, row[0].text, row[1].text, row[2].text
          end
        end
      end
    end
  end

  def write
    File.write @static_file, self.build
  end
end