How to use CacheFile method of lang Package

Best Gauge code snippet using lang.CacheFile

pygments.go

Source:pygments.go Github

copy

Full Screen

1// Copyright © 2013-14 Steve Francia <spf@spf13.com>.2//3// Licensed under the Simple Public 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 at6// http://opensource.org/licenses/Simple-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.13package helpers14import (15 "bytes"16 "crypto/sha1"17 "fmt"18 "github.com/spf13/hugo/hugofs"19 jww "github.com/spf13/jwalterweatherman"20 "github.com/spf13/viper"21 "io"22 "io/ioutil"23 "os/exec"24 "path/filepath"25 "sort"26 "strings"27)28const pygmentsBin = "pygmentize"29// HasPygments checks to see if Pygments is installed and available30// on the system.31func HasPygments() bool {32 if _, err := exec.LookPath(pygmentsBin); err != nil {33 return false34 }35 return true36}37// Highlight takes some code and returns highlighted code.38func Highlight(code, lang, optsStr string) string {39 if !HasPygments() {40 jww.WARN.Println("Highlighting requires Pygments to be installed and in the path")41 return code42 }43 options, err := parsePygmentsOpts(optsStr)44 if err != nil {45 jww.ERROR.Print(err.Error())46 return code47 }48 // Try to read from cache first49 hash := sha1.New()50 io.WriteString(hash, code)51 io.WriteString(hash, lang)52 io.WriteString(hash, options)53 fs := hugofs.OsFs54 cacheDir := viper.GetString("CacheDir")55 var cachefile string56 if cacheDir != "" {57 cachefile = filepath.Join(cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil)))58 exists, err := Exists(cachefile, fs)59 if err != nil {60 jww.ERROR.Print(err.Error())61 return code62 }63 if exists {64 f, err := fs.Open(cachefile)65 if err != nil {66 jww.ERROR.Print(err.Error())67 return code68 }69 s, err := ioutil.ReadAll(f)70 if err != nil {71 jww.ERROR.Print(err.Error())72 return code73 }74 return string(s)75 }76 }77 // No cache file, render and cache it78 var out bytes.Buffer79 var stderr bytes.Buffer80 var langOpt string81 if lang == "" {82 langOpt = "-g" // Try guessing the language83 } else {84 langOpt = "-l" + lang85 }86 cmd := exec.Command(pygmentsBin, langOpt, "-fhtml", "-O", options)87 cmd.Stdin = strings.NewReader(code)88 cmd.Stdout = &out89 cmd.Stderr = &stderr90 if err := cmd.Run(); err != nil {91 jww.ERROR.Print(stderr.String())92 return code93 }94 str := out.String()95 // inject code tag into Pygments output96 if lang != "" && strings.Contains(str, "<pre>") {97 codeTag := fmt.Sprintf(`<pre><code class="language-%s" data-lang="%s">`, lang, lang)98 str = strings.Replace(str, "<pre>", codeTag, 1)99 str = strings.Replace(str, "</pre>", "</code></pre>", 1)100 }101 if cachefile != "" {102 // Write cache file103 if err := WriteToDisk(cachefile, strings.NewReader(str), fs); err != nil {104 jww.ERROR.Print(stderr.String())105 }106 }107 return str108}109var pygmentsKeywords = make(map[string]bool)110func init() {111 pygmentsKeywords["style"] = true112 pygmentsKeywords["encoding"] = true113 pygmentsKeywords["noclasses"] = true114 pygmentsKeywords["hl_lines"] = true115 pygmentsKeywords["linenos"] = true116 pygmentsKeywords["classprefix"] = true117}118func parsePygmentsOpts(in string) (string, error) {119 in = strings.Trim(in, " ")120 style := viper.GetString("PygmentsStyle")121 noclasses := "true"122 if viper.GetBool("PygmentsUseClasses") {123 noclasses = "false"124 }125 if len(in) == 0 {126 return fmt.Sprintf("style=%s,noclasses=%s,encoding=utf8", style, noclasses), nil127 }128 options := make(map[string]string)129 o := strings.Split(in, ",")130 for _, v := range o {131 keyVal := strings.Split(v, "=")132 key := strings.ToLower(strings.Trim(keyVal[0], " "))133 if len(keyVal) != 2 || !pygmentsKeywords[key] {134 return "", fmt.Errorf("invalid Pygments option: %s", key)135 }136 options[key] = keyVal[1]137 }138 if _, ok := options["style"]; !ok {139 options["style"] = style140 }141 if _, ok := options["noclasses"]; !ok {142 options["noclasses"] = noclasses143 }144 if _, ok := options["encoding"]; !ok {145 options["encoding"] = "utf8"146 }147 var keys []string148 for k := range options {149 keys = append(keys, k)150 }151 sort.Strings(keys)152 var optionsStr string153 for i, k := range keys {154 optionsStr += fmt.Sprintf("%s=%s", k, options[k])155 if i < len(options)-1 {156 optionsStr += ","157 }158 }159 return optionsStr, nil160}...

Full Screen

Full Screen

base.go

Source:base.go Github

copy

Full Screen

1package controllers2import (3 "io/ioutil"4 "net/http"5 "os"6 "path"7 "strings"8 "github.com/astaxie/beego"9 "github.com/beego/website/models"10)11type BaseController struct {12 beego.Controller13 Lang string14 // static file cache support15 cacheFile string16 cacheEnable bool17 cacheBody []byte18}19func (bc *BaseController) Prepare() {20 if bc.Lang = bc.Ctx.GetCookie("lang"); bc.Lang == "" {21 bc.Lang = "en-US"22 }23 bc.Data["Lang"] = bc.Lang24 bc.Data["I18n"] = models.I18ns[strings.ToLower(bc.Lang)]25 bc.cacheFile = path.Join(beego.AppConfig.DefaultString("cache_dir", "_cache"), bc.Lang, bc.Ctx.Input.URL())26 if !strings.HasSuffix(bc.cacheFile, ".html") {27 bc.cacheFile += ".html"28 }29 if fi, _ := os.Stat(bc.cacheFile); fi != nil && !fi.IsDir() {30 http.ServeFile(bc.Ctx.ResponseWriter, bc.Ctx.Request, bc.cacheFile)31 bc.StopRun()32 }33 bc.Layout = "layout.html"34 bc.setProperTemplateFile()35 bc.Data["Title"] = "Beego Website"36 bc.Data["HomeNav"] = models.HomeNav37 bc.Data["DocNav"] = models.DocNav38 bc.Data["Type"] = "page"39}40func (bc *BaseController) setProperTemplateFile() {41 p := bc.Ctx.Request.URL.Path42 paths := strings.Split(p, "/")43 if len(paths) >= 2 {44 bc.TplName = paths[1]45 }46 if bc.TplName == "" {47 bc.TplName = "index.html"48 }49 if !strings.HasSuffix(bc.TplName, ".html") {50 bc.TplName += ".html"51 }52}53func (bc *BaseController) Render() error {54 if renderBytes, _ := bc.RenderBytes(); len(renderBytes) > 0 {55 bc.cacheBody = renderBytes56 bc.cacheEnable = beego.AppConfig.DefaultBool("cache_flag", false)57 }58 return bc.Controller.Render()59}60func (bc *BaseController) Finish() {61 if bc.cacheEnable {62 os.MkdirAll(path.Dir(bc.cacheFile), os.ModePerm)63 ioutil.WriteFile(bc.cacheFile, bc.cacheBody, os.ModePerm) // todo : error handle64 }65}...

Full Screen

Full Screen

CacheFile

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gol.CacheFile("testfile", "test data")4 fmt.Println(gol.ReadFile("testfile"))5}6import (7func main() {8 gol.CacheFile("testfile", "t

Full Screen

Full Screen

CacheFile

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("1.go")4 if err != nil {5 fmt.Println("Error opening file")6 }7 src, err := scanner.ScanFile(fset, "1.go", f, 0)8 if err != nil {9 fmt.Println("Error scanning file")10 }11 f, err = parser.ParseFile(fset, "1.go", src, 0)12 if err != nil {13 fmt.Println("Error parsing file")14 }15 lang.CacheFile(fset, f)16}17import (18func main() {19 f, err := os.Open("2.go")20 if err != nil {21 fmt.Println("Error opening file")22 }23 src, err := scanner.ScanFile(fset, "2.go", f, 0)24 if err != nil {25 fmt.Println("Error scanning file")26 }27 f, err = parser.ParseFile(fset, "2.go", src, 0)28 if err != nil {29 fmt.Println("Error parsing file")30 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful