github.com/cayleygraph/cayley@v0.7.7/internal/http/docs.go (about)

     1  // Copyright 2014 The Cayley Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package http
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"strings"
    21  
    22  	"github.com/gobuffalo/packr/v2"
    23  	"github.com/julienschmidt/httprouter"
    24  	"github.com/russross/blackfriday"
    25  )
    26  
    27  const markdownCSS = "/static/css/docs.css"
    28  
    29  var docsBox = packr.New("Docs", "../../docs")
    30  
    31  func markdownWithCSS(input []byte, title string) []byte {
    32  	// set up the HTML renderer
    33  	htmlFlags := 0
    34  	htmlFlags |= blackfriday.HTML_USE_XHTML
    35  	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
    36  	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
    37  	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
    38  	htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
    39  	renderer := blackfriday.HtmlRenderer(htmlFlags, title, markdownCSS)
    40  
    41  	// set up the parser
    42  	extensions := 0
    43  	//extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
    44  	extensions |= blackfriday.EXTENSION_TABLES
    45  	extensions |= blackfriday.EXTENSION_FENCED_CODE
    46  	extensions |= blackfriday.EXTENSION_AUTOLINK
    47  	extensions |= blackfriday.EXTENSION_STRIKETHROUGH
    48  	//extensions |= blackfriday.EXTENSION_SPACE_HEADERS
    49  	extensions |= blackfriday.EXTENSION_HEADER_IDS
    50  	extensions |= blackfriday.EXTENSION_LAX_HTML_BLOCKS
    51  
    52  	return blackfriday.Markdown(input, renderer, extensions)
    53  }
    54  
    55  func serveDocPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
    56  	docpage := params.ByName("docpage")
    57  	if docpage == "" {
    58  		docpage = "Index"
    59  	}
    60  	if !strings.HasSuffix(docpage, ".md") {
    61  		docpage += ".md"
    62  	}
    63  	data, err := docsBox.Find(docpage)
    64  	if err != nil {
    65  		http.Error(w, err.Error(), http.StatusNoContent)
    66  		return
    67  	}
    68  	output := markdownWithCSS(data, fmt.Sprintf("Cayley Docs - %s", docpage))
    69  	fmt.Fprint(w, string(output))
    70  }