vdoc: remove http doc server (#7984)

pull/7996/head
Larpon 2021-01-09 22:47:58 +01:00 committed by GitHub
parent e79695e8fc
commit 362c21de06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 139 deletions

View File

@ -127,23 +127,24 @@ fn (vd VDoc) render_search_index(out Output) {
os.write_file(out_file_path, js_search_index.str() + js_search_data.str())
}
fn (mut vd VDoc) render_static_html(serve_via_http bool, out Output) {
fn (mut vd VDoc) render_static_html(out Output) {
vd.assets = {
'doc_css': vd.get_resource(css_js_assets[0], true, out)
'normalize_css': vd.get_resource(css_js_assets[1], true, out)
'doc_js': vd.get_resource(css_js_assets[2], !serve_via_http, out)
'dark_mode_js': vd.get_resource(css_js_assets[3], !serve_via_http, out)
'light_icon': vd.get_resource('light.svg', true, out)
'dark_icon': vd.get_resource('dark.svg', true, out)
'menu_icon': vd.get_resource('menu.svg', true, out)
'arrow_icon': vd.get_resource('arrow.svg', true, out)
'doc_css': vd.get_resource(css_js_assets[0], out)
'normalize_css': vd.get_resource(css_js_assets[1], out)
'doc_js': vd.get_resource(css_js_assets[2], out)
'dark_mode_js': vd.get_resource(css_js_assets[3], out)
'light_icon': vd.get_resource('light.svg', out)
'dark_icon': vd.get_resource('dark.svg', out)
'menu_icon': vd.get_resource('menu.svg', out)
'arrow_icon': vd.get_resource('arrow.svg', out)
}
}
fn (vd VDoc) get_resource(name string, minify bool, out Output) string {
fn (vd VDoc) get_resource(name string, out Output) string {
cfg := vd.cfg
path := os.join_path(res_path, name)
mut res := os.read_file(path) or { panic('vdoc: could not read $path') }
/*
if minify {
if name.ends_with('.js') {
res = js_compress(res)
@ -151,6 +152,7 @@ fn (vd VDoc) get_resource(name string, minify bool, out Output) string {
res = res.split_into_lines().map(it.trim_space()).join('')
}
}
*/
// TODO: Make SVG inline for now
if cfg.inline_assets || path.ends_with('.svg') {
return res
@ -482,6 +484,7 @@ fn html_tag_escape(str string) string {
return str.replace_each(['<', '&lt;', '>', '&gt;'])
}
/*
fn js_compress(str string) string {
mut js := strings.new_builder(200)
lines := str.split_into_lines()
@ -503,7 +506,7 @@ fn js_compress(str string) string {
js.free()
return js_str
}
*/
fn write_toc(dn doc.DocNode, mut toc strings.Builder) {
mut toc_slug := if dn.name.len == 0 || dn.content.len == 0 { '' } else { slug(dn.name) }
if toc_slug == '' && dn.children.len > 0 {

View File

@ -1,95 +0,0 @@
module main
import io
import net
import strings
fn (mut vd VDoc) serve_html(out Output) {
cfg := vd.cfg
if out.typ == .html {
vd.render_static_html(true, out)
}
docs := vd.render(out)
dkeys := docs.keys()
if dkeys.len < 1 {
eprintln('no documentation created, the module has no `pub` functions')
exit(1)
}
def_name := docs.keys()[0]
server_url := 'http://localhost:' + cfg.server_port.str()
server := net.listen_tcp(cfg.server_port) or { panic(err) }
println('Serving docs on: $server_url')
if cfg.open_docs {
open_url(server_url)
}
content_type := match out.typ {
.html { 'text/html' }
.markdown { 'text/markdown' }
.json { 'application/json' }
else { 'text/plain' }
}
server_context := VdocHttpServerContext{
docs: docs
content_type: content_type
default_filename: def_name
}
for {
mut conn := server.accept() or {
server.close() or { }
panic(err)
}
handle_http_connection(mut conn, server_context)
conn.close() or { eprintln('error closing the connection: $err') }
}
}
struct VdocHttpServerContext {
docs map[string]string
content_type string
default_filename string
}
fn handle_http_connection(mut con net.TcpConn, ctx &VdocHttpServerContext) {
mut reader := io.new_buffered_reader(reader: io.make_reader(con))
first_line := reader.read_line() or {
send_http_response(mut con, 501, ctx.content_type, 'bad request')
return
}
request_parts := first_line.split(' ')
if request_parts.len != 3 {
send_http_response(mut con, 501, ctx.content_type, 'bad request')
return
}
urlpath := request_parts[1]
filename := if urlpath == '/' {
ctx.default_filename.trim_left('/')
} else {
urlpath.trim_left('/')
}
if ctx.docs[filename].len == 0 {
send_http_response(mut con, 404, ctx.content_type, 'file not found')
return
}
send_http_response(mut con, 200, ctx.content_type, ctx.docs[filename])
}
fn send_http_response(mut con net.TcpConn, http_code int, content_type string, html string) {
content_length := html.len.str()
shttp_code := http_code.str()
mut http_response := strings.new_builder(20000)
http_response.write('HTTP/1.1 ')
http_response.write(shttp_code)
http_response.write(' OK\r\n')
http_response.write('Server: VDoc\r\n')
http_response.write('Content-Type: ')
http_response.write(content_type)
http_response.write('\r\n')
http_response.write('Content-Length: ')
http_response.write(content_length)
http_response.write('\r\n')
http_response.write('Connection: close\r\n')
http_response.write('\r\n')
http_response.write(html)
sresponse := http_response.str()
con.write_str(sresponse) or { eprintln('error sending http response: $err') }
}

View File

@ -47,14 +47,11 @@ mut:
local_filename string
local_pos int
show_loc bool // for plaintext
serve_http bool // for html
is_multi bool
is_vlib bool
is_verbose bool
include_readme bool
include_examples bool = true
open_docs bool
server_port int = 8046
inline_assets bool
no_timestamp bool
output_path string
@ -336,14 +333,10 @@ fn (mut vd VDoc) generate_docs_from_file() {
docs << vd.docs.filter(it.head.name != 'builtin')
vd.docs = docs
}
if cfg.serve_http {
vd.serve_html(out)
return
}
vd.vprintln('Rendering docs...')
if out.path.len == 0 || out.path == 'stdout' {
if out.typ == .html {
vd.render_static_html(cfg.serve_http, out)
vd.render_static_html(out)
}
outputs := vd.render(out)
if outputs.len == 0 {
@ -370,7 +363,7 @@ fn (mut vd VDoc) generate_docs_from_file() {
}
}
if out.typ == .html {
vd.render_static_html(cfg.serve_http, out)
vd.render_static_html(out)
}
vd.render_parallel(out)
println('Creating search index...')
@ -433,9 +426,6 @@ fn parse_arguments(args []string) Config {
cfg.output_path = if opath == 'stdout' { opath } else { os.real_path(opath) }
i++
}
'-open' {
cfg.open_docs = true
}
'-pos' {
if !cfg.is_local {
eprintln('vdoc: `-pos` is only allowed with `-filename` flag.')
@ -444,27 +434,6 @@ fn parse_arguments(args []string) Config {
cfg.local_pos = cmdline.option(current_args, '-pos', '').int()
i++
}
'-p' {
s_port := cmdline.option(current_args, '-p', '')
s_port_int := s_port.int()
if s_port.len == 0 {
eprintln('vdoc: No port number specified on "-p".')
exit(1)
}
if s_port != s_port_int.str() {
eprintln('vdoc: Invalid port number.')
exit(1)
}
cfg.server_port = s_port_int
i++
}
'-s' {
cfg.inline_assets = true
cfg.serve_http = true
if cfg.output_type == .unset {
cfg.output_type = .html
}
}
'-no-timestamp' {
cfg.no_timestamp = true
}