mirror of
https://github.com/kataras/iris.git
synced 2026-01-24 12:25:57 +00:00
7.0.1 - https://github.com/iris-contrib/community-board/issues/1 https://github.com/iris-contrib/community-board/issues/2
Read HISTORY.md Former-commit-id: 82df2d266055818ffafe0ba66b58cf4ed9089922
This commit is contained in:
11
internal/cmd/gen/website/recipe/example/example.go
Normal file
11
internal/cmd/gen/website/recipe/example/example.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package example
|
||||
|
||||
// Example defines the example link.
|
||||
type Example struct {
|
||||
Name string // i.e: Hello World
|
||||
DataSource string // i.e: https://raw.githubusercontent.com/iris-contrib/examples/master/hello-world.go
|
||||
Children []Example // if has children the data source is not a source file, it's just a folder, its the template's H2 tag.
|
||||
// needed for the raw templates, we can do a simple func but lets keep it simple, it's a small template file.
|
||||
HasChildren bool
|
||||
HasNotChildren bool
|
||||
}
|
||||
129
internal/cmd/gen/website/recipe/example/parser.go
Normal file
129
internal/cmd/gen/website/recipe/example/parser.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/kataras/iris/core/errors"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/russross/blackfriday"
|
||||
)
|
||||
|
||||
// Parse will try to parse and return all examples.
|
||||
// The input parameter "branch" is used to build
|
||||
// the raw..iris-contrib/examples/$branch/
|
||||
// but it should be the same with
|
||||
// the kataras/iris/$branch/ for consistency.
|
||||
func Parse(branch string) (examples []Example, err error) {
|
||||
var (
|
||||
contentsURL = "https://raw.githubusercontent.com/iris-contrib/examples/" + branch
|
||||
tableOfContents = "Table of contents"
|
||||
sanitizeMarkdown = true
|
||||
)
|
||||
|
||||
// get the raw markdown
|
||||
readmeURL := contentsURL + "/README.md"
|
||||
res, err := http.Get(readmeURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
markdownContents, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// convert it to html
|
||||
htmlContents := &bytes.Buffer{}
|
||||
htmlContentsFromMarkdown := blackfriday.MarkdownCommon(markdownContents)
|
||||
|
||||
if len(htmlContentsFromMarkdown) == 0 {
|
||||
return nil, errors.New("empty html")
|
||||
}
|
||||
|
||||
if sanitizeMarkdown {
|
||||
markdownContents = bluemonday.UGCPolicy().SanitizeBytes(markdownContents)
|
||||
}
|
||||
|
||||
htmlContents.Write(htmlContentsFromMarkdown)
|
||||
// println("html contents: " + htmlContents.String())
|
||||
// get the document from the html
|
||||
readme, err := goquery.NewDocumentFromReader(htmlContents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// or with just one line (but may break if we add another h2,
|
||||
// so I will do it with the hard and un-readable way for now)
|
||||
// readme.Find("h2").First().NextAllFiltered("ul").Children().Text()
|
||||
|
||||
// find the header of Table Of Contents, we will need it to take its
|
||||
// next ul, which should be the examples list.
|
||||
var tableOfContentsHeader *goquery.Selection
|
||||
readme.Find("h2").EachWithBreak(func(_ int, n *goquery.Selection) bool {
|
||||
if nodeContents := n.Text(); nodeContents == tableOfContents {
|
||||
tableOfContentsHeader = n
|
||||
return false // break
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if tableOfContentsHeader == nil {
|
||||
return nil, errors.New("table of contents not found using: " + tableOfContents)
|
||||
}
|
||||
|
||||
// get the list of the examples
|
||||
tableOfContentsUL := tableOfContentsHeader.NextFiltered("ul")
|
||||
if tableOfContentsUL == nil {
|
||||
return nil, errors.New("table of contents list not found")
|
||||
}
|
||||
|
||||
// iterate over categories example's <a href ...>...</a>
|
||||
tableOfContentsUL.Children().EachWithBreak(func(_ int, li *goquery.Selection) bool {
|
||||
exampleHrefLink := li.Children().First()
|
||||
if exampleHrefLink == nil {
|
||||
err = errors.New("example link href is nil, source: " + li.Text())
|
||||
return false // break on first failure
|
||||
}
|
||||
|
||||
name := exampleHrefLink.Text()
|
||||
|
||||
sourcelink, _ := li.Find("a").First().Attr("href")
|
||||
hasChildren := !strings.HasSuffix(sourcelink, ".go")
|
||||
|
||||
example := Example{
|
||||
Name: name,
|
||||
DataSource: contentsURL + "/" + sourcelink,
|
||||
HasChildren: hasChildren,
|
||||
HasNotChildren: !hasChildren,
|
||||
}
|
||||
|
||||
// search for sub examples
|
||||
if hasChildren {
|
||||
li.Find("ul").Children().EachWithBreak(func(_ int, liExample *goquery.Selection) bool {
|
||||
name := liExample.Text()
|
||||
liHref := liExample.Find("a").First()
|
||||
sourcelink, ok := liHref.Attr("href")
|
||||
if !ok {
|
||||
err = errors.New(name + "'s source couldn't be found")
|
||||
return false
|
||||
}
|
||||
|
||||
subExample := Example{
|
||||
Name: name,
|
||||
DataSource: contentsURL + "/" + sourcelink,
|
||||
}
|
||||
|
||||
example.Children = append(example.Children, subExample)
|
||||
return true
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
examples = append(examples, example)
|
||||
return true
|
||||
})
|
||||
return examples, err
|
||||
}
|
||||
30
internal/cmd/gen/website/recipe/recipe.go
Normal file
30
internal/cmd/gen/website/recipe/recipe.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/internal/cmd/gen/website/recipe/example"
|
||||
)
|
||||
|
||||
type Recipe struct {
|
||||
Branch string // i.e "master", "v6"...
|
||||
Examples []example.Example
|
||||
}
|
||||
|
||||
// NewRecipe accepts the "branch", i.e: "master", "v6", "v7"...
|
||||
// and returns a new Recipe pointer with its generated and parsed examples.
|
||||
func NewRecipe(branch string) (*Recipe, error) {
|
||||
if branch == "" {
|
||||
branch = "master"
|
||||
}
|
||||
|
||||
examples, err := example.Parse(branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &Recipe{
|
||||
Branch: branch,
|
||||
Examples: examples,
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
149
internal/cmd/gen/website/recipe/recipe.html
Normal file
149
internal/cmd/gen/website/recipe/recipe.html
Normal file
@@ -0,0 +1,149 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>Iris</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="description" content="Iris-go - The fastest backend web framework for Go.">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
|
||||
|
||||
<meta property="og:type" content="article">
|
||||
<meta property="og:title" content="Iris">
|
||||
<meta property="og:description" content="Iris-go - The fastest backend web framework for Go.">
|
||||
<meta property="og:image" content="http://iris-go.com/images/icon.svg">
|
||||
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:title" content="Iris">
|
||||
<meta name="twitter:description" content="Iris-go - The fastest backend web framework for Go.">
|
||||
<meta name="twitter:image" content="http://iris-go.com/images/icon.svg">
|
||||
|
||||
<link rel="icon" href="/images/favicon.ico" type="image/x-icon">
|
||||
|
||||
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600|Roboto Mono' rel='stylesheet' type='text/css'>
|
||||
<link href='//fonts.googleapis.com/css?family=Dosis:500&text=Iris' rel='stylesheet' type='text/css'>
|
||||
<link href='/css/prism.css' rel='stylesheet' type='text/css'>
|
||||
<link href='/css/terminal.css' rel='stylesheet' type='text/css'>
|
||||
|
||||
<!-- main page styles -->
|
||||
<link rel="stylesheet" href="/css/page.css">
|
||||
|
||||
<!-- this needs to be loaded before guide's inline scripts -->
|
||||
<script>
|
||||
window.PAGE_TYPE = "Recipe"
|
||||
</script>
|
||||
|
||||
<!-- ga -->
|
||||
<script>
|
||||
(function (i, s, o, g, r, a, m) {
|
||||
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
|
||||
(i[r].q = i[r].q || []).push(arguments)
|
||||
}, i[r].l = 1 * new Date(); a = s.createElement(o),
|
||||
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
|
||||
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
|
||||
|
||||
ga('create', 'UA-46045347-6', 'iris-go.com');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body class="docs">
|
||||
<div id="mobile-bar">
|
||||
<a class="menu-button"></a>
|
||||
<a class="logo" href="/"></a>
|
||||
</div>
|
||||
<div id="header">
|
||||
<a id="logo" href="/">
|
||||
<img src="/images/logo.png">
|
||||
<span>IRIS</span>
|
||||
</a>
|
||||
<ul id="nav">
|
||||
<li><a href="/{{.Branch}}/start/" class="nav-link">Start</a></li>
|
||||
<li><a href="/{{.Branch}}/recipe/" class="nav-link current">Recipe</a></li>
|
||||
<li><a href="/{{.Branch}}/blogs/" class="nav-link">Blogs</a></li>
|
||||
<li><a target="_blank" href="https://kataras.rocket.chat/channel/iris" class="nav-link">Chat</a></li>
|
||||
<li><a class="donate" style="color:#ff6666;" target="_blank" href="https://github.com/kataras/iris#buy-me-a-cup-of-coffee">Donate</a></li>
|
||||
<!--
|
||||
<li>
|
||||
<form id="search-form">
|
||||
<input type="text" id="search-query-nav"
|
||||
class="search-query st-default-search-input"/>
|
||||
</form>
|
||||
</li>
|
||||
-->
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
||||
<div id="main" class="fix-sidebar">
|
||||
|
||||
|
||||
<div class="sidebar">
|
||||
<ul class="main-menu">
|
||||
<li><a href="/{{.Branch}}/start/" class="nav-link">Start</a></li>
|
||||
<li><a href="/{{.Branch}}/recipe/" class="nav-link current">Recipe</a></li>
|
||||
<li><a href="/{{.Branch}}/blogs/" class="nav-link">Blogs</a></li>
|
||||
<li><a target="_blank" href="https://kataras.rocket.chat/channel/iris" class="nav-link">Chat</a></li>
|
||||
<li><a target="_blank" class="donate" style="color:#ff6666;" href="https://github.com/kataras/iris#buy-me-a-cup-of-coffee">Donate</a></li>
|
||||
</ul>
|
||||
<div class="list">
|
||||
<h2>
|
||||
Recipe
|
||||
</h2>
|
||||
<ul class="menu-root">
|
||||
<li>
|
||||
<a href="/{{.Branch}}/recipe/index.html" class="sidebar-link current"></a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content Recipe with-sidebar ">
|
||||
{{ range $key, $example := .Examples -}}
|
||||
{{ if $example.HasChildren }}
|
||||
<h2 id="{{$example.Name}}">
|
||||
<a href="#{{$example.Name}}" class="headerlink" title="{{$example.Name}}"></a>
|
||||
{{$example.Name}}
|
||||
</h2>
|
||||
{{ range $key, $child := $example.Children -}}
|
||||
<h3 id="{{ $child.Name }}">
|
||||
<a href="#{{ $child.Name }}" class="headerlink" title="{{ $child.Name }}"></a>
|
||||
{{ $child.Name }}
|
||||
</h3>
|
||||
<pre data-src="{{ $child.DataSource }}" data-visible="true" class="line-numbers codepre"></pre>
|
||||
{{- end }} {{- end }}
|
||||
|
||||
{{ if $example.HasNotChildren }}
|
||||
<h2 id="{{ $example.Name }}">
|
||||
<a href="#{{ $example.Name }}" class="headerlink" title="{{ $example.Name }}"></a>
|
||||
{{ $example.Name }}
|
||||
</h2>
|
||||
<pre data-src="{{ $example.DataSource }}" data-visible="true" class="line-numbers codepre"></pre>
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
<div class="footer">
|
||||
Contribute to the documentation?
|
||||
<a target="_blank" href="https://github.com/iris-contrib/website/blob/gh-pages/{{.Branch}}/recipe/index.html" >
|
||||
Edit this page on Github!
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<script src="/js/smooth-scroll.min.js"></script>
|
||||
|
||||
<script async defer src="https://buttons.github.io/buttons.js"></script>
|
||||
<!-- main custom script for sidebars, version selects etc. -->
|
||||
<script src="/js/css.escape.js"></script>
|
||||
<script src="/js/common.js"></script>
|
||||
<script src="/js/prism.js"></script>
|
||||
<!-- fastclick -->
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
FastClick.attach(document.body)
|
||||
}, false)
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user