Mercurial Hosting > freedit
comparison src/lib/Bbcode.luan @ 20:3ea49246d6a7
bbcode work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 13 Jul 2022 22:00:00 -0600 |
parents | 0edde02b908c |
children | 33731231093a |
comparison
equal
deleted
inserted
replaced
19:da006d1c1eba | 20:3ea49246d6a7 |
---|---|
14 local String = require "luan:String.luan" | 14 local String = require "luan:String.luan" |
15 local gsub = String.gsub or error() | 15 local gsub = String.gsub or error() |
16 local User = require "site:/lib/User.luan" | 16 local User = require "site:/lib/User.luan" |
17 local Shared = require "site:/lib/Shared.luan" | 17 local Shared = require "site:/lib/Shared.luan" |
18 local list_to_set = Shared.list_to_set or error() | 18 local list_to_set = Shared.list_to_set or error() |
19 local to_list = Shared.to_list or error() | |
19 local Logging = require "luan:logging/Logging.luan" | 20 local Logging = require "luan:logging/Logging.luan" |
20 local logger = Logging.logger "Bbcode" | 21 local logger = Logging.logger "Bbcode" |
21 | 22 |
22 | 23 |
23 local Bbcode = {} | 24 local Bbcode = {} |
24 | 25 |
25 local to_html | 26 local to_html |
26 local html = {} | 27 local html = {} |
27 | 28 |
28 function html.b(bbcode) | 29 function html.b(bbcode,options) |
29 %><b><% to_html(bbcode.contents) %></b><% | 30 %><b><% to_html(bbcode.contents,options) %></b><% |
30 end | 31 end |
31 | 32 |
32 function html.i(bbcode) | 33 function html.i(bbcode,options) |
33 %><i><% to_html(bbcode.contents) %></i><% | 34 %><i><% to_html(bbcode.contents,options) %></i><% |
34 end | 35 end |
35 | 36 |
36 function html.u(bbcode) | 37 function html.u(bbcode,options) |
37 %><u><% to_html(bbcode.contents) %></u><% | 38 %><u><% to_html(bbcode.contents,options) %></u><% |
38 end | 39 end |
39 | 40 |
40 function html.url(bbcode) | 41 function html.s(bbcode,options) |
42 %><s><% to_html(bbcode.contents,options) %></s><% | |
43 end | |
44 | |
45 function html.sup(bbcode,options) | |
46 %><sup><% to_html(bbcode.contents,options) %></sup><% | |
47 end | |
48 | |
49 function html.brackets(bbcode,options) | |
50 %>[<% to_html(bbcode.contents,options) %>]<% | |
51 end | |
52 | |
53 function html.url(bbcode,options) | |
41 local url = bbcode.param | 54 local url = bbcode.param |
42 if url == nil then | 55 if url == nil then |
43 url = html_encode(bbcode.contents) | 56 url = html_encode(bbcode.contents) |
44 %><a href="<%=url%>"><%=url%></a><% | 57 %><a href="<%=url%>"><%=url%></a><% |
45 else | 58 else |
46 url = html_encode(url) | 59 url = html_encode(url) |
47 %><a href="<%=url%>"><% to_html(bbcode.contents) %></a><% | 60 %><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><% |
48 end | 61 end |
49 end | 62 end |
50 | 63 |
51 function html.code(bbcode) | 64 function html.code(bbcode,options) |
52 %><code><%= html_encode(bbcode.contents) %></code><% | 65 local s = gsub(bbcode.contents,[[^\n]],"") |
53 end | 66 %><code><%= html_encode(s) %></code><% |
54 | 67 options.strip_newline = true |
55 function html.img(bbcode) | 68 end |
69 | |
70 function html.img(bbcode,options) | |
56 %><img src="<%= html_encode(bbcode.contents) %>"><% | 71 %><img src="<%= html_encode(bbcode.contents) %>"><% |
57 end | 72 end |
58 | 73 |
59 function html.color(bbcode) | 74 function html.color(bbcode,options) |
60 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents) %></span><% | 75 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><% |
61 end | 76 end |
62 | 77 |
63 function html.size(bbcode) | 78 function html.size(bbcode,options) |
64 %><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents) %></span><% | 79 %><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents,options) %></span><% |
65 end | 80 end |
66 | 81 |
67 function html.quote(bbcode) | 82 function html.quote(bbcode,options) |
68 %><blockquote><% | 83 %><blockquote><% |
69 local user_name = bbcode.param | 84 local user_name = bbcode.param |
70 if user_name ~= nil then | 85 if user_name ~= nil then |
71 local user = User.get_by_name(user_name) | 86 local user = User.get_by_name(user_name) |
72 if user == nil then | 87 if user == nil then |
73 %><%= user_name %> wrote:<% | 88 %><%= user_name %> wrote:<% |
74 else | 89 else |
75 %><a href="/user_something"><%= user_name %></a> wrote:<% | 90 %><a href="/user_something"><%= user_name %></a> wrote:<% |
76 end | 91 end |
77 end | 92 else |
78 to_html(bbcode.contents) | 93 options.strip_newline = true |
94 end | |
95 to_html(bbcode.contents,options) | |
79 %></blockquote><% | 96 %></blockquote><% |
80 end | 97 options.strip_newline = true |
81 | 98 end |
82 function html.video(bbcode) | 99 |
100 function html.video(bbcode,options) | |
83 local url = html_encode(bbcode.contents) | 101 local url = html_encode(bbcode.contents) |
84 local site = bbcode.site | 102 local site = bbcode.site |
85 if site == "youtube" then | 103 if site == "youtube" then |
86 %><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><% | 104 %><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><% |
87 local start = bbcode.start | 105 local start = bbcode.start |
94 else | 112 else |
95 %><a href="<%=url%>"><%=url%></a><% | 113 %><a href="<%=url%>"><%=url%></a><% |
96 end | 114 end |
97 end | 115 end |
98 | 116 |
99 function to_html(bbcode) | 117 local function list_to_html(bbcode,options) |
118 local list = to_list(bbcode.contents) | |
119 for _, item in ipairs(list) do | |
120 if type(item) == "table" and item.name == "li" then | |
121 %><li><% to_html(item.contents,options) %></li><% | |
122 end | |
123 end | |
124 options.strip_newline = true | |
125 end | |
126 | |
127 function html.ul(bbcode,options) | |
128 %><ul><% | |
129 list_to_html(bbcode,options) | |
130 %></ul><% | |
131 end | |
132 | |
133 function html.ol(bbcode,options) | |
134 %><ol><% | |
135 list_to_html(bbcode,options) | |
136 %></ol><% | |
137 end | |
138 | |
139 function to_html(bbcode,options) | |
140 if options.strip_newline then | |
141 if type(bbcode) == "string" then | |
142 bbcode = gsub(bbcode,[[^\n]],"") | |
143 end | |
144 options.strip_newline = false | |
145 end | |
100 if type(bbcode) == "string" then | 146 if type(bbcode) == "string" then |
101 %><%= html_encode(bbcode) %><% | 147 %><%= html_encode(bbcode) %><% |
102 else | 148 else |
103 type(bbcode) == "table" or error() | 149 type(bbcode) == "table" or error() |
104 if is_list(bbcode) then | 150 if is_list(bbcode) then |
105 for _, v in ipairs(bbcode) do | 151 for _, v in ipairs(bbcode) do |
106 to_html(v) | 152 to_html(v,options) |
107 end | 153 end |
108 else | 154 else |
109 local fn = html[bbcode.name] or error(bbcode.name.." not handled") | 155 local fn = html[bbcode.name] or error(bbcode.name.." not handled") |
110 fn(bbcode) | 156 fn(bbcode,options) |
111 end | 157 end |
112 end | 158 end |
113 end | 159 end |
114 | 160 |
115 function Bbcode.to_html(bbcode) | 161 function Bbcode.to_html(bbcode) |
116 bbcode = bbcode_parse(bbcode) | 162 bbcode = bbcode_parse(bbcode) |
117 %><div message><% to_html(bbcode) %></div><% | 163 %><div message><% |
164 to_html(bbcode,{strip_newline=false}) | |
165 %></div><% | |
118 end | 166 end |
119 | 167 |
120 | 168 |
121 local doesnt_nest = list_to_set{ | 169 local doesnt_nest = list_to_set{ |
122 "url" | 170 "url" |
146 if doesnt_nest[name] then | 194 if doesnt_nest[name] then |
147 %><%=bbcode.contents%><% | 195 %><%=bbcode.contents%><% |
148 else | 196 else |
149 preprocess(bbcode.contents) | 197 preprocess(bbcode.contents) |
150 end | 198 end |
151 %>[/<%=name%>]<% | 199 if name == "code" and param ~= nil then |
200 %>[/<%=name%>=<%=param%>]<% | |
201 else | |
202 %>[/<%=name%>]<% | |
203 end | |
152 end | 204 end |
153 end | 205 end |
154 end | 206 end |
155 | 207 |
156 function Bbcode.preprocess(bbcode) | 208 function Bbcode.preprocess(bbcode) |