// MarkFriday combines a fs.FilReader with a markdown processor which then pipes into a fs.FileWriter to save the output func MarkFriday(m MarkConfig) flux.Reactor { if m.Ext == "" { m.Ext = ".md" } var markdown flux.Reactor if m.Sanitize { markdown = BlackMonday() } else { markdown = BlackFriday() } reader := fs.FileReader() // reader.React(flux.SimpleMuxer(func(root flux.Reactor, data interface{}) { // log.Printf("reader %s", data) // }), true) writer := fs.FileWriter(func(path string) string { var dir string if m.PathMux != nil { dir = m.PathMux(m, path) } else { //get the current directory of the path cdir := filepath.Dir(path) //if we have a preset folder replace it if m.SaveDir != "" { cdir = m.SaveDir } // strip out the directory from the path and only use the base name base := filepath.Base(path) //combine with the dir for the final path dir = filepath.Join(cdir, base) } //grab our own extension ext := strings.Replace(m.Ext, ".", "", -1) //strip off the extension and add ours return strings.Replace(dir, filepath.Ext(dir), fmt.Sprintf(".%s", ext), -1) }) stack := flux.ReactStack(reader) stack.Bind(FileRead2RenderFile(), true) stack.Bind(markdown, true) stack.Bind(RenderFile2FileWrite(), true) stack.Bind(MutateFileWrite(m.BeforeWrite), true) stack.Bind(writer, true) return stack }
// MarkFridayStream returns a flux.Reactor that takes the given config and generates a markdown auto-converter, when // it recieves any signals,it will stream down each file and convert the markdown input and save into the desired output path func MarkFridayStream(m MarkStreamConfig) (flux.Reactor, error) { streamer, err := fs.StreamListings(fs.ListingConfig{ Path: m.InputDir, Validator: m.Validator, Mux: m.Mux, }) if err != nil { return nil, err } absPath, _ := filepath.Abs(m.InputDir) markdown := MarkFriday(MarkConfig{ SaveDir: m.SaveDir, Ext: m.Ext, Sanitize: m.Sanitize, BeforeWrite: m.BeforeWrite, PathMux: func(m MarkConfig, path string) string { //we find the index of the absolute path we need to index index := strings.Index(path, absPath) // log.Printf("absolute: indexing path %s with %s -> %d", path, absPath, index) //if we found one then strip the absolute path and combine with SaveDir if index != -1 { return filepath.Join(m.SaveDir, strings.Replace(path, absPath, "./", 1)) } //we didnt find one so we find the base, backtrack a step,strip that off and combine with the SaveDir base := filepath.Join(filepath.Base(path), "..") index = strings.Index(path, base) // log.Printf("fallback: indexing path %s with %s -> %d", path, base, index) return filepath.Join(m.SaveDir, strings.Replace(path, base, "./", 1)) }, }) stack := flux.ReactStack(streamer) stack.Bind(markdown, true) return stack, nil }
// JSLauncher returns a reactor that on receiving a signal builds the gopherjs package as giving in the config and writes it out using a FileWriter func JSLauncher(config JSBuildConfig) flux.Reactor { stack := flux.ReactStack(JSBuildLauncher(config)) stack.Bind(fs.FileWriter(nil), true) return stack }