Skip to content

Commit

Permalink
Replicating specs folder structure while converting to HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
kashishm committed Oct 28, 2016
1 parent ac24ea2 commit 184dfda
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 62 deletions.
4 changes: 0 additions & 4 deletions constant/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,3 @@ var IndexContent = `<center>
<div class="specs"></div>
`
var DataFile = `<script src="data.js"></script>`

var IncludeIndex = "<li class=\"nav\"><a href=\"index.html\">=</a></li>"

var IncludeCSS = `<link rel="stylesheet" type="text/css" href="style.css">`
73 changes: 73 additions & 0 deletions conv/conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package conv

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/getgauge/spectacle/gauge_messages"
"github.com/getgauge/spectacle/util"
"github.com/golang-commonmark/markdown"
)

const (
dotHtml = ".html"
dotCSS = ".css"
quotes = `""''`
linkTemplate = "<li class=\"nav\"><a href=\"%s\">%s</a></li>"
htmlTemplate = `%s%s<ul id="navigation"><center>%s%s%s</center>`
includeIndex = "<li class=\"nav\"><a href=\"%s\">=</a></li>"
IncludeCSS = `<link rel="stylesheet" type="text/css" href="%s">`
indexHtml = "index.html"
StyleCSS = "style.css"
)

var outDir = util.GetOutDir()
var projectRoot = util.GetProjectRoot()

func ConvertFile(file string, files []string, index int) {
md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true), markdown.Quotes(quotes))
input, err := ioutil.ReadFile(file)
util.Fatal(fmt.Sprintf("Error while reading %s file", file), err)
output := md.RenderToString(input)
var next, prev string
if index+1 < len(files) {
next = fmt.Sprintf(linkTemplate, getRelFilePath(file, files[index+1], dotHtml), ">")
}
if index != 0 {
prev = fmt.Sprintf(linkTemplate, getRelFilePath(file, files[index-1], dotHtml), "<")
}
toc := fmt.Sprintf(includeIndex, getRelFilePath(file, filepath.Join(projectRoot, indexHtml), dotHtml))
style := fmt.Sprintf(IncludeCSS, getRelFilePath(file, filepath.Join(projectRoot, StyleCSS), dotCSS))
output = fmt.Sprintf(htmlTemplate, style, output, prev, toc, next)
relPath := getRelPath(projectRoot, file)
util.CreateDirectory(filepath.Join(outDir, filepath.Dir(relPath)))
name := strings.TrimSuffix(relPath, filepath.Ext(relPath)) + dotHtml
f, err := os.Create(filepath.Join(outDir, name))
util.Fatal(fmt.Sprintf("Error while creating %s file", filepath.Join(outDir, name)), err)
f.Write([]byte(output))
f.Close()
}

func GetSpecs(m *gauge_messages.SpecsResponse) []*gauge_messages.ProtoSpec {
specs := make([]*gauge_messages.ProtoSpec, 0)
for _, d := range m.Details {
if d.Spec != nil {
specs = append(specs, d.Spec)
}
}
return specs
}

func getRelFilePath(f, f1, ext string) string {
base := getRelPath(filepath.Dir(f), filepath.Dir(f1)) + string(filepath.Separator) + filepath.Base(f1)
return strings.TrimSuffix(base, filepath.Ext(f1)) + ext
}

func getRelPath(f, f1 string) string {
p, err := filepath.Rel(f, f1)
util.Fatal("Cannot get relative path", err)
return p
}
17 changes: 11 additions & 6 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/golang/protobuf/proto"
)

var projectRoot = util.GetProjectRoot()

type specInfo struct {
Path string `json:"path"`
Name string `json:"name"`
Expand All @@ -27,10 +29,12 @@ type scenarioInfo struct {
func WriteJS(specs []*gauge_messages.ProtoSpec, files []string, outDir string, fileExtn string) {
specs = sortSpecs(specs, files)
specScenariosMap, tags := getSpecsScenariosMap(specs, fileExtn)
f, _ := os.Create(outDir + string(filepath.Separator) + "data.js")
f, err := os.Create(outDir + string(filepath.Separator) + "data.js")
util.Fatal("Unable to create data.js file", err)
f.WriteString(fmt.Sprintf("%s\n%s", tags, specScenariosMap))
f.Close()
f, _ = os.Create(outDir + string(filepath.Separator) + "index.js")
f, err = os.Create(outDir + string(filepath.Separator) + "index.js")
util.Fatal("Unable to create index.js file", err)
f.WriteString(constant.IndexJSContent)
f.Close()
}
Expand Down Expand Up @@ -58,7 +62,8 @@ func getSpecsScenariosMap(specs []*gauge_messages.ProtoSpec, fileExtn string) (s
var specsInfos []specInfo
tags := make(map[string]bool)
for _, spec := range specs {
fileName := strings.TrimSuffix(filepath.Base(spec.GetFileName()), filepath.Ext(spec.GetFileName()))
relPath, _ := filepath.Rel(projectRoot, spec.GetFileName())
fileName := strings.TrimSuffix(relPath, filepath.Ext(spec.GetFileName()))
si := specInfo{Path: fileName + fileExtn, Name: spec.GetSpecHeading(), Scenarios: make([]scenarioInfo, 0)}
addTags(spec.Tags, tags)
for _, item := range spec.Items {
Expand All @@ -73,12 +78,12 @@ func getSpecsScenariosMap(specs []*gauge_messages.ProtoSpec, fileExtn string) (s
}
specsInfos = append(specsInfos, si)
}
json, err := json.Marshal(specsInfos)
j, err := json.Marshal(specsInfos)
if err != nil {
fmt.Println("Cannot convert specs to HTML. Reason: %s", err.Error())
fmt.Printf("Cannot convert specs to HTML. Reason: %s\n", err.Error())
return "", ""
}
return fmt.Sprintf("var specs = %s", string(json)), fmt.Sprintf("var tags = [%s]", strings.Join(getUniqueTags(tags), ", "))
return fmt.Sprintf("var specs = %s", string(j)), fmt.Sprintf("var tags = [%s]", strings.Join(getUniqueTags(tags), ", "))
}

func addTags(tags []string, tagsMap map[string]bool) {
Expand Down
61 changes: 16 additions & 45 deletions spectacle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,63 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/getgauge/spectacle/constant"
"github.com/getgauge/spectacle/gauge_messages"
"github.com/getgauge/spectacle/conv"
"github.com/getgauge/spectacle/json"
"github.com/getgauge/spectacle/processor"
"github.com/getgauge/spectacle/util"
"github.com/golang-commonmark/markdown"
)

const (
html = ".html"
dotHtml = ".html"
localhost = "localhost"
gaugeSpecsDir = "GAUGE_SPEC_DIRS"
gaugeApiPort = "GAUGE_API_PORT"
space = " "
indexFile = "index.html"
quotes = `""''`
linkTemplate = "<li class=\"nav\"><a href=\"%s\">%s</a></li>"
htmlTemplate = `%s%s<ul id="navigation"><center>%s%s%s</center>`
styleCSS = "style.css"
)

var outDir = util.GetOutDir()
var projectRoot = util.GetProjectRoot()

func main() {
var files []string
for _, arg := range strings.Split(os.Getenv(gaugeSpecsDir), space) {
files = append(files, util.GetFiles(arg)...)
}
p, _ := processor.NewMessageProcessor(localhost, os.Getenv(gaugeApiPort))
msg, _ := p.GetSpecsResponse()
p, err := processor.NewMessageProcessor(localhost, os.Getenv(gaugeApiPort))
util.Fatal("Cannot connect to Gauge API", err)
msg, err := p.GetSpecsResponse()
util.Fatal("Cannot connect to Gauge API", err)
p.Connection.Close()
util.CreateDirectory(outDir)
json.WriteJS(getSpecs(msg.SpecsResponse), files, outDir, html)
json.WriteJS(conv.GetSpecs(msg.SpecsResponse), files, outDir, dotHtml)
writeCSS()
var lastSpec string
for i, file := range files {
convertFile(file, files, i, &lastSpec)
conv.ConvertFile(file, files, i)
}
createIndex()
fmt.Printf("Succesfully converted specs to html => %s\n", outDir)
}

func getSpecs(m *gauge_messages.SpecsResponse) []*gauge_messages.ProtoSpec {
specs := make([]*gauge_messages.ProtoSpec, 0)
for _, d := range m.Details {
if d.Spec != nil {
specs = append(specs, d.Spec)
}
}
return specs
}

func convertFile(file string, files []string, index int, lastSpec *string) {
md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true), markdown.Quotes(quotes))
input, _ := ioutil.ReadFile(file)
output := md.RenderToString(input)
var next, previous string
if index+1 < len(files) {
next = fmt.Sprintf(linkTemplate, strings.TrimSuffix(filepath.Base(files[index+1]),
filepath.Ext(files[index+1]))+html, ">")
}
if index != 0 {
previous = fmt.Sprintf(linkTemplate, *lastSpec, "<")
}
output = fmt.Sprintf(htmlTemplate, constant.IncludeCSS, output, previous, constant.IncludeIndex, next)
name := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
f, _ := os.Create(outDir + string(filepath.Separator) + name + html)
f.Write([]byte(output))
f.Close()
*lastSpec = name + html
}

func createIndex() {
f, _ := os.Create(outDir + string(filepath.Separator) + indexFile)
input := constant.IncludeCSS + constant.DataFile + fmt.Sprintf(constant.IndexContent, filepath.Base(util.GetProjectRoot())) + constant.IndexJS
f, err := os.Create(outDir + string(filepath.Separator) + indexFile)
util.Fatal("Unable to create index.html", err)
style := fmt.Sprintf(conv.IncludeCSS, conv.StyleCSS)
header := fmt.Sprintf(constant.IndexContent, filepath.Base(projectRoot))
input := style + constant.DataFile + header + constant.IndexJS
f.WriteString(input)
f.Close()
}

func writeCSS() {
f, _ := os.Create(outDir + string(filepath.Separator) + styleCSS)
f, err := os.Create(outDir + string(filepath.Separator) + styleCSS)
util.Fatal("Error while creating style.css file", err)
f.Write([]byte(constant.CSS))
f.Close()
}
17 changes: 10 additions & 7 deletions util/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func findFilesInDir(dirPath string, isValidFile func(path string) bool) []string
}

func findFilesIn(dirRoot string, isValidFile func(path string) bool) []string {
absRoot, _ := filepath.Abs(dirRoot)
absRoot := getAbsPath(dirRoot)
files := findFilesInDir(absRoot, isValidFile)
return files
}
Expand All @@ -60,15 +60,18 @@ func GetFiles(path string) []string {
if dirExists(path) {
specFiles = append(specFiles, findFilesIn(path, isValidSpecExtension)...)
} else if fileExists(path) && isValidSpecExtension(path) {
f, _ := filepath.Abs(path)
specFiles = append(specFiles, f)
specFiles = append(specFiles, getAbsPath(path))
}
return specFiles
}

func CreateDirectory(dir string) {
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Printf("Failed to create directory %s: %s\n", dir, err)
os.Exit(1)
}
err := os.MkdirAll(dir, 0755)
Fatal(fmt.Sprintf("Failed to create directory %s", dir), err)
}

func getAbsPath(path string) string {
f, err := filepath.Abs(path)
Fatal("Cannot get absolute path", err)
return f
}
7 changes: 7 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ func GetOutDir() string {
func GetProjectRoot() string {
return projectRoot
}

func Fatal(message string, err error) {
if err != nil {
fmt.Printf("%s. Error: %s", message, err.Error())
os.Exit(1)
}
}

0 comments on commit 184dfda

Please sign in to comment.