Giter Club home page Giter Club logo

embedmd's Introduction

Build Status Go Report Card

embedmd

Are you tired of copy pasting your code into your README.md file, just to forget about it later on and have unsynced copies? Or even worse, code that does not even compile?

Then embedmd is for you!

embedmd embeds files or fractions of files into Markdown files. It does so by searching embedmd commands, which are a subset of the Markdown syntax for comments. This means they are invisible when Markdown is rendered, so they can be kept in the file as pointers to the origin of the embedded text.

The command receives a list of Markdown files. If no list is given, the command reads from the standard input.

The format of an embedmd command is:

[embedmd]:# (pathOrURL language /start regexp/ /end regexp/)

The embedded code will be extracted from the file at pathOrURL, which can either be a relative path to a file in the local file system (using always forward slashes as directory separator) or a URL starting with http:// or https://. If the pathOrURL is a URL the tool will fetch the content in that URL. The embedded content starts at the first line that matches /start regexp/ and finishes at the first line matching /end regexp/.

Omitting the the second regular expression will embed only the piece of text that matches /regexp/:

[embedmd]:# (pathOrURL language /regexp/)

To embed the whole line matching a regular expression you can use:

[embedmd]:# (pathOrURL language /.*regexp.*/)

To embed from a point to the end you should use:

[embedmd]:# (pathOrURL language /start regexp/ $)

To embed a whole file, omit both regular expressions:

[embedmd]:# (pathOrURL language)

You can omit the language in any of the previous commands, and the extension of the file will be used for the snippet syntax highlighting.

This works when the file extensions matches the name of the language (like Go files, since .go matches go). However, this will fail with other files like .md whose language name is markdown.

[embedmd]:# (file.ext)

Installation

You can install Go by following these instructions.

embedmd is written in Go, so if you have Go installed you can install it with go get:

go get github.com/campoy/embedmd

This will download the code, compile it, and leave an embedmd binary in $GOPATH/bin.

Eventually, and if there's enough interest, I will provide binaries for every OS and architecture out there ... eventually.

Usage:

Given the two files in sample:

hello.go:

// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("Hello, there, it is", time.Now())
}

docs.md:

# A hello world in Go

Go is very simple, here you can see a whole "hello, world" program.

[embedmd]:# (hello.go)

We can try to embed a file from a directory.

[embedmd]:# (test/hello.go /func main/ $)

You always start with a `package` statement like:

[embedmd]:# (hello.go /package.*/)

Followed by an `import` statement:

[embedmd]:# (hello.go /import/ /\)/)

You can also see how to get the current time:

[embedmd]:# (hello.go /time\.[^)]*\)/)

Flags

  • -w: Executing embedmd -w docs.md will modify docs.md and add the corresponding code snippets, as shown in sample/result.md.

  • -d: Executing embedmd -d docs.md will display the difference between the contents of docs.md and the output of embedmd docs.md.

Disclaimer

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

embedmd's People

Contributors

appleboy avatar bbrks avatar broady avatar campoy avatar dmitshur avatar dvrkps avatar getaaron avatar lc0 avatar yanzay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

embedmd's Issues

Feature: diff with exit code

As a user, during my CI tests, it would be nice if I could run something like embedmd diff input.md output.md and my tests would fail if there is a diff detected.

The command does not output the result

  1. Install go:
> go version
version go1.8 windows/amd64
  1. Compile embedmd
> go get github.com/campoy/embedmd
  1. Create folder and cope there two files docs.md & hello.go

  2. Execute command to get result of output as written in README usages at https://github.com/campoy/embedmd:

> embedmd -d docs.md
docs.md:unexpected format for diff output:

> embedmd -w docs.md

Nothing is produced. No any result.md files. What could be wrong ?

Any plans to make it available as a library?

embedmd looks great and I may want to use it, but I doubt I'd use it standalone. Instead, I already have markdownfmt which I run on Markdown files and I may want to incorporate it there.

But to do so, I'd need to be able to import embedmd as a library. All the code is currently inside the single package which is a command, that doesn't lend itself to importing.

I just wanted to ask if you had any plans to split it into a library + command? Similar to:

Thanks!

support **/*.md even when not on zsh

currently failing some tests on travis CI because embedmd **/*.md doesn't match any files

I was able to reproduce the problem by running the program from bash:

bash-3.2$ ls *.md
CONTRIBUTING.md	README.md
bash-3.2$ embedmd -d **/*.md
**/*.md:open **/*.md: no such file or directory

Regexp flags or \S,\s commands

Hey there,
thanks for the awesome repo!
I'm trying to match a codeblock that looks like this

equation_(a,b,c,d)
    $ (some_condition
     $ (some_other_condition)) ..

My start regex to match the equation looks like this
(?s)equation_.*?\.\., so it's effectively using the single line flag. However, it looks like embedmd does not support this kind of flags.
My other thought was to substitute these flags by something like equation_[\S\s]*?\.\. , but then embedmd also throws an error saying \S was not escaped.

Does anyone have a recommendation for me?
Thanks in advance!

Contiguous code sections misshandled

Currently contiguous code sections cause "unbalanced code section" errors because we miss the start of the second section.

```go
hello
```
```go
bye
```

Clarify path separator used in filename in the spec.

The embedmd command is currently specified/documented as:

The format of an embedmd command is:

[embedmd]:# (filename language /start regexp/ /end regexp/)

The embedded code will be extracted from the file filename, starting at the first line that matches /start regexp/ and finishing at the first line matching /end regexp/.

This is underspecified, and I have the following questions about the filename:

  • Can the filename refer to a file inside a directory? If so, what should the path separator be used? Would it be the OS-specific path separator, such as \ on Windows or / on Linux? Or would it always be slash (/, U+002F) separated like URLs are, no matter what OS you're on?
  • Are absolute paths supported too, or only relative paths?

So, if I have a file hello.go in a subfolder code, which of these should I write if I'm on Windows:

[embedmd]:# (code/hello.go /package.*/)
[embedmd]:# (code\hello.go /package.*/)

Or is it invalid/unsupported to have the hello.go file inside a subfolder? Edit: According to this test case, subfolders are supported.

I think resolving this issue is a prerequisite for resolving #10.

Also, in case it's not obvious, I really hope you choose to go with always /-separated paths, because that's the only way Markdown documents written on any OS would be compatible/interchangeable.

Allow substitution via regexp

I would like to be able to write some substitutions / transformations for the outputted text produced by embedmd.

Use case: Include example_test.go in readme, but replace package my_test with package main and func Example with func main.

This could be expressed as s/find/replace/, and when used in practice:

[embedmd]:# (example_test.go s/^package .*_test$/package main/)

Due to the s-prefix we can differentiate this from start / end and allow multiple occurances:

OK: [embedmd]:# (pathOrURL language s/find/replace/ /start regexp/ /end regexp/)
OK: [embedmd]:# (pathOrURL language s/find/replace/ s/find2/replace2/ /start regexp/ /end regexp/)
OK: [embedmd]:# (pathOrURL language /start regexp/ /end regexp/ s/find/replace/)
ERROR: [embedmd]:# (pathOrURL language /start regexp/ s/find/replace/ /end regexp/)

Thoughts?

EDIT: I don't mind implementing if this is accepted.

EDIT 2: For reference, an alternative (with go:generate and sed) can be seen here, but it's far from ideal.

support a flag to exclude matching lines

I would like to use a single file containing many nondescript snippets like

client, err := storage.NewClient(ctx)

Currently the regexps used to match will appear in the output, forcing me to use regexps that are fragile (may match multiple snippets, may fail to match if I change the snippet).

I'd rather write

// START AUTH
client, err := storage.NewClient(ctx)
// END AUTH

So I can match the markers but not have them in the output.

I propose a -x flag that provides this behavior.

features needed for Go snippet insertion

My application is inserting snippets from compilable Go code into a README.md. I wasn't able to use embedmd for a couple of reasons.

  • I want to exclude the matching regexps, so I can write comments in the Go code that delimit the snippet. I don't want to match the snippet code itself, because that is fragile—it can change. See #46.

  • I really want to exclude the line the regexp is on. Not a big deal, just add .* to the regexp, but I need to do that for every one, so it's a little error-prone.

  • The snippets are typically inside functions, so they have a leading tab. I need to remove this tab from each line so that my Markdown code blocks are not indented. No way to do this in the current embedmd model.

  • Another whitespace issue: at top-level, gofmt puts a blank line between code and a following comment. If that comment is an end-of-snippet delimiter, I need to drop that final blank line, or it will end up in my markdown. (I want to preserve blank lines inside the snippet, however.)

  • Sometimes one wants to write ... in a snippet as a placeholder. E.g.

    tokenSource := ...
    client = New(ctx, tokenSource)

    Since that isn't legal Go, I need something that will transform legal Go into it. E.g. replace ELLIPSIS and _ = ELLIPSIS with ....

Allow tabs to be rewritten to spaces

Tabs work great in code because we can set their width to whatever we prefer in our editor, but in blog posts they usually just lead to code running off the screen.

It would be cool if you could translate tabs to 2 spaces, or set a tab width somehow. Is this a PR you would be willing to consider?

Binary releases

"if there's enough interest, I will provide binaries for every OS and architecture out there ... eventually."

You may be interested in cloud-gox

asciidoc alternative

I know this in not the right place to ask, but maybe someone will direct me

I like embedmd alot and I want to use it for asciidoc documents..
can someone point me to a fork that does it for asciidoc?

It should be easy to match "to the next blank line"

An entry such as:

Given input.txt:

foo
bar
baz

qux
zot

With input:
[embedmd]:# (input.txt /foo/ /\n\n/)
embeds the text

foo
bar
baz

including the final blank line.

Obviously I could make the regexp match 'baz' instead, but I think wanting to embed chunks of lines should be easier.

Unable to match escaped forward-slashes

I want to match between a set of multi-line C-style (/* */) style comments which get displayed at the package level in the godoc tool. For example: doc.go source -> godoc preview

But I get an unbalanced / error when trying to escape values with the regexps: /\/\*/ or /\*\//

I'd imagine this would also cause issues when attempting to match a file path, or URL.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.