func LoadSchema(uri string, localCopy bool) (sd *Schema, err error) { var protocol, localPath string var rc io.ReadCloser if pos := strings.Index(uri, protSep); pos < 0 { protocol = "http" + protSep } else { protocol = uri[:pos+len(protSep)] uri = uri[pos+len(protSep):] } if localCopy { if localPath = filepath.Join(PkgGen.BaseCodePath, uri); !ufs.FileExists(localPath) { if err = ufs.EnsureDirExists(filepath.Dir(localPath)); err == nil { err = unet.DownloadFile(protocol+uri, localPath) } } if err == nil { if sd, err = loadSchemaFile(localPath, uri); sd != nil { sd.loadLocalPath = localPath } } } else if rc, err = unet.OpenRemoteFile(protocol + uri); err == nil { defer rc.Close() sd, err = loadSchema(rc, uri, "") } return }
func serveTemplatedContent(w http.ResponseWriter, r *http.Request) { urlPath := strings.Trim(r.URL.Path, "/") // First handle static files (robots.txt / sitemap.xml / favicon.ico etc.) etc via 'static' folder if ufs.FileExists(filepath.Join(dir("static"), urlPath)) { fileServer.ServeHTTP(w, r) return } var ( err error filePath string fileData []byte isMarkdown bool ) if filePath = filepath.Join(dir("contents"), urlPath) + ".html"; !ufs.FileExists(filePath) { if filePath = filepath.Join(dir("contents"), urlPath, "index.html"); !ufs.FileExists(filePath) { isMarkdown = true if filePath = filepath.Join(dir("contents"), urlPath) + ".md"; !ufs.FileExists(filePath) { filePath = filepath.Join(dir("contents"), urlPath, "index.md") } } } pc := NewPageContext(r, urlPath) if len(filePath) > 0 && ufs.FileExists(filePath) { if fileData, err = ioutil.ReadFile(filePath); err == nil { var tmpl *template.Template if pos := bytes.Index(fileData, []byte("{{")); pos >= 0 { if bytes.Index(fileData, []byte("}}")) > pos { if _, ok := SiteData.pageTemplates[filePath]; !ok { SiteData.pageTemplates[filePath] = nil DirWatch.WatchIn(filepath.Dir(filePath), ustr.Pattern(filepath.Base(filePath)), false, func(fullPath string) { SiteData.pageTemplates[fullPath] = nil }) } if tmpl = SiteData.pageTemplates[filePath]; tmpl == nil { var err error tmpl, err = template.ParseFiles(filePath) if err == nil { SiteData.pageTemplates[filePath] = tmpl } else { tmpl, err = template.New("pt_error").Parse(fmt.Sprintf("ERROR loading template %s:\t%+v", filePath, err)) } } } } if tmpl != nil { var buf bytes.Buffer if err := tmpl.Execute(&buf, pc); err != nil { fileData = []byte(err.Error()) } else { fileData = buf.Bytes() } } if isMarkdown { pc.HtmlContent = template.HTML(markdown.MarkdownCommon(fileData)) } else { pc.HtmlContent = template.HTML(fileData) } } } else { pc.HtmlContent = "404 Not Found" } pos1, pos2 := strings.Index(string(pc.HtmlContent), "<h2>"), strings.LastIndex(string(pc.HtmlContent), "</h2>") if pos1 >= 0 && pos2 > pos1 { docTitle := string(pc.HtmlContent[:pos2]) if pos2 = strings.Index(docTitle, "</h2>"); pos2 < 0 { pc.PageTitle = docTitle[pos1+4:] } } if err == nil { err = SiteData.mainTemplate.Execute(w, pc) } else { w.Write([]byte(err.Error())) } }