cmd/golangorg: split out internal/codewalk [generated]
This code is not likely to ever change, so it's nice to stash elsewhere.
CL generated by the script below.
[git-generate]
cd cmd/golangorg
rf '
mv codewalk.go golang.org/x/website/internal/codewalk
'
cd ../../internal/codewalk
rf '
mv CodewalkServer Server
mv NewCodewalkServer NewServer
mv Codewalk codewalk
mv Codestep codestep
'
Change-Id: If16a7dc9214a8d44ce42bc8e7e6ccd502b718c0e
Reviewed-on: https://go-review.googlesource.com/c/website/+/317661
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Website-Publish: Russ Cox <rsc@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/golangorg/handlers.go b/cmd/golangorg/handlers.go
index 6000ac3..85b985d 100644
--- a/cmd/golangorg/handlers.go
+++ b/cmd/golangorg/handlers.go
@@ -14,6 +14,7 @@
"net/http"
"strings"
+ "golang.org/x/website/internal/codewalk"
"golang.org/x/website/internal/env"
"golang.org/x/website/internal/redirect"
"golang.org/x/website/internal/web"
@@ -73,7 +74,7 @@
}
mux := http.NewServeMux()
mux.Handle("/", site)
- mux.Handle("/doc/codewalk/", NewCodewalkServer(fsys, site))
+ mux.Handle("/doc/codewalk/", codewalk.NewServer(fsys, site))
mux.Handle("/fmt", http.HandlerFunc(fmtHandler))
mux.Handle("/x/", http.HandlerFunc(xHandler))
redirect.Register(mux)
diff --git a/cmd/golangorg/codewalk.go b/internal/codewalk/codewalk.go
similarity index 92%
rename from cmd/golangorg/codewalk.go
rename to internal/codewalk/codewalk.go
index 00a5ea5..532a092 100644
--- a/cmd/golangorg/codewalk.go
+++ b/internal/codewalk/codewalk.go
@@ -13,7 +13,7 @@
// That page is itself a codewalk; the source code for it is
// _content/doc/codewalk/codewalk.xml.
-package main
+package codewalk
import (
"encoding/xml"
@@ -26,7 +26,6 @@
"net/http"
"os"
"path"
- pathpkg "path"
"regexp"
"sort"
"strconv"
@@ -36,17 +35,17 @@
"golang.org/x/website/internal/web"
)
-type CodewalkServer struct {
+type Server struct {
fsys fs.FS
site *web.Site
}
-func NewCodewalkServer(fsys fs.FS, site *web.Site) *CodewalkServer {
- return &CodewalkServer{fsys, site}
+func NewServer(fsys fs.FS, site *web.Site) *Server {
+ return &Server{fsys, site}
}
// Handler for /doc/codewalk/ and below.
-func (s *CodewalkServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
relpath := path.Clean(r.URL.Path[1:])
r.ParseForm()
@@ -92,7 +91,7 @@
}
func redir(w http.ResponseWriter, r *http.Request) (redirected bool) {
- canonical := pathpkg.Clean(r.URL.Path)
+ canonical := path.Clean(r.URL.Path)
if !strings.HasSuffix(canonical, "/") {
canonical += "/"
}
@@ -105,15 +104,15 @@
return
}
-// A Codewalk represents a single codewalk read from an XML file.
-type Codewalk struct {
+// A codewalk represents a single codewalk read from an XML file.
+type codewalk struct {
Title string `xml:"title,attr"`
File []string `xml:"file"`
- Step []*Codestep `xml:"step"`
+ Step []*codestep `xml:"step"`
}
-// A Codestep is a single step in a codewalk.
-type Codestep struct {
+// A codestep is a single step in a codewalk.
+type codestep struct {
// Filled in from XML
Src string `xml:"src,attr"`
Title string `xml:"title,attr"`
@@ -129,13 +128,13 @@
Data []byte
}
-func (c *Codestep) HTML() template.HTML {
+func (c *codestep) HTML() template.HTML {
return template.HTML(c.XML)
}
// String method for printing in template.
// Formats file address nicely.
-func (st *Codestep) String() string {
+func (st *codestep) String() string {
s := st.File
if st.Lo != 0 || st.Hi != 0 {
s += fmt.Sprintf(":%d", st.Lo)
@@ -147,13 +146,13 @@
}
// loadCodewalk reads a codewalk from the named XML file.
-func (s *CodewalkServer) loadCodewalk(filename string) (*Codewalk, error) {
+func (s *Server) loadCodewalk(filename string) (*codewalk, error) {
f, err := s.fsys.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
- cw := new(Codewalk)
+ cw := new(codewalk)
d := xml.NewDecoder(f)
d.Entity = xml.HTMLEntity
err = d.Decode(cw)
@@ -210,7 +209,7 @@
// codewalkDir serves the codewalk directory listing.
// It scans the directory for subdirectories or files named *.xml
// and prepares a table.
-func (s *CodewalkServer) codewalkDir(w http.ResponseWriter, r *http.Request, relpath string) {
+func (s *Server) codewalkDir(w http.ResponseWriter, r *http.Request, relpath string) {
type elem struct {
Name string
Title string
@@ -249,7 +248,7 @@
// in the response. This format is used for the middle window pane
// of the codewalk pages. It is a separate iframe and does not get
// the usual godoc HTML wrapper.
-func (s *CodewalkServer) codewalkFileprint(w http.ResponseWriter, r *http.Request, f string) {
+func (s *Server) codewalkFileprint(w http.ResponseWriter, r *http.Request, f string) {
relpath := strings.Trim(path.Clean(f), "/")
data, err := fs.ReadFile(s.fsys, relpath)
if err != nil {