4
vendor/github.com/campoy/embedmd/.gitignore
generated
vendored
Normal file
4
vendor/github.com/campoy/embedmd/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
.vscode/
|
||||
*.test
|
||||
embed
|
||||
.DS_Store
|
9
vendor/github.com/campoy/embedmd/.travis.yml
generated
vendored
Normal file
9
vendor/github.com/campoy/embedmd/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
- 1.x
|
||||
- master
|
||||
script:
|
||||
- go test ./...
|
202
vendor/github.com/campoy/embedmd/LICENSE
generated
vendored
Normal file
202
vendor/github.com/campoy/embedmd/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
152
vendor/github.com/campoy/embedmd/README.md
generated
vendored
Normal file
152
vendor/github.com/campoy/embedmd/README.md
generated
vendored
Normal file
@@ -0,0 +1,152 @@
|
||||
[](https://travis-ci.org/campoy/embedmd) [](https://goreportcard.com/report/github.com/campoy/embedmd)
|
||||
|
||||
|
||||
# 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:
|
||||
|
||||
```Markdown
|
||||
[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/`:
|
||||
|
||||
```Markdown
|
||||
[embedmd]:# (pathOrURL language /regexp/)
|
||||
```
|
||||
|
||||
To embed the whole line matching a regular expression you can use:
|
||||
|
||||
```Markdown
|
||||
[embedmd]:# (pathOrURL language /.*regexp.*/)
|
||||
```
|
||||
|
||||
To embed from a point to the end you should use:
|
||||
|
||||
```Markdown
|
||||
[embedmd]:# (pathOrURL language /start regexp/ $)
|
||||
```
|
||||
|
||||
To embed a whole file, omit both regular expressions:
|
||||
|
||||
```Markdown
|
||||
[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`.
|
||||
|
||||
```Markdown
|
||||
[embedmd]:# (file.ext)
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
> You can install Go by following [these instructions](https://golang.org/doc/install).
|
||||
|
||||
`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](sample):
|
||||
|
||||
*hello.go:*
|
||||
|
||||
[embedmd]:# (sample/hello.go)
|
||||
```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:*
|
||||
|
||||
[embedmd]:# (sample/docs.md Markdown /./ /embedmd.*time.*/)
|
||||
```Markdown
|
||||
# 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](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.
|
101
vendor/github.com/campoy/embedmd/embedmd/command.go
generated
vendored
Normal file
101
vendor/github.com/campoy/embedmd/embedmd/command.go
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright 2016 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package embedmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type command struct {
|
||||
path, lang string
|
||||
start, end *string
|
||||
}
|
||||
|
||||
func parseCommand(s string) (*command, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' {
|
||||
return nil, errors.New("argument list should be in parenthesis")
|
||||
}
|
||||
|
||||
args, err := fields(s[1 : len(s)-1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(args) == 0 {
|
||||
return nil, errors.New("missing file name")
|
||||
}
|
||||
|
||||
cmd := &command{path: args[0]}
|
||||
args = args[1:]
|
||||
if len(args) > 0 && args[0][0] != '/' {
|
||||
cmd.lang, args = args[0], args[1:]
|
||||
} else {
|
||||
ext := filepath.Ext(cmd.path[1:])
|
||||
if len(ext) == 0 {
|
||||
return nil, errors.New("language is required when file has no extension")
|
||||
}
|
||||
cmd.lang = ext[1:]
|
||||
}
|
||||
|
||||
switch {
|
||||
case len(args) == 1:
|
||||
cmd.start = &args[0]
|
||||
case len(args) == 2:
|
||||
cmd.start, cmd.end = &args[0], &args[1]
|
||||
case len(args) > 2:
|
||||
return nil, errors.New("too many arguments")
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// fields returns a list of the groups of text separated by blanks,
|
||||
// keeping all text surrounded by / as a group.
|
||||
func fields(s string) ([]string, error) {
|
||||
var args []string
|
||||
|
||||
for s = strings.TrimSpace(s); len(s) > 0; s = strings.TrimSpace(s) {
|
||||
if s[0] == '/' {
|
||||
sep := nextSlash(s[1:])
|
||||
if sep < 0 {
|
||||
return nil, errors.New("unbalanced /")
|
||||
}
|
||||
args, s = append(args, s[:sep+2]), s[sep+2:]
|
||||
} else {
|
||||
sep := strings.IndexByte(s[1:], ' ')
|
||||
if sep < 0 {
|
||||
return append(args, s), nil
|
||||
}
|
||||
args, s = append(args, s[:sep+1]), s[sep+1:]
|
||||
}
|
||||
}
|
||||
|
||||
return args, nil
|
||||
}
|
||||
|
||||
// nextSlash will find the index of the next unescaped slash in a string.
|
||||
func nextSlash(s string) int {
|
||||
for sep := 0; ; sep++ {
|
||||
i := strings.IndexByte(s[sep:], '/')
|
||||
if i < 0 {
|
||||
return -1
|
||||
}
|
||||
sep += i
|
||||
if sep == 0 || s[sep-1] != '\\' {
|
||||
return sep
|
||||
}
|
||||
}
|
||||
}
|
51
vendor/github.com/campoy/embedmd/embedmd/content.go
generated
vendored
Normal file
51
vendor/github.com/campoy/embedmd/embedmd/content.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2016 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package embedmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fetcher provides an abstraction on a file system.
|
||||
// The Fetch function is called anytime some content needs to be fetched.
|
||||
// For now this includes files and URLs.
|
||||
// The first parameter is the base directory that could be used to resolve
|
||||
// relative paths. This base directory will be ignored for absolute paths,
|
||||
// such as URLs.
|
||||
type Fetcher interface {
|
||||
Fetch(dir, path string) ([]byte, error)
|
||||
}
|
||||
|
||||
type fetcher struct{}
|
||||
|
||||
func (fetcher) Fetch(dir, path string) ([]byte, error) {
|
||||
if !strings.HasPrefix(path, "http://") && !strings.HasPrefix(path, "https://") {
|
||||
path = filepath.Join(dir, filepath.FromSlash(path))
|
||||
return ioutil.ReadFile(path)
|
||||
}
|
||||
|
||||
res, err := http.Get(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("status %s", res.Status)
|
||||
}
|
||||
return ioutil.ReadAll(res.Body)
|
||||
}
|
153
vendor/github.com/campoy/embedmd/embedmd/embedmd.go
generated
vendored
Normal file
153
vendor/github.com/campoy/embedmd/embedmd/embedmd.go
generated
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright 2016 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package embedmd provides a single function, Process, that parses markdown
|
||||
// searching for markdown comments.
|
||||
//
|
||||
// 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.*\n/)
|
||||
//
|
||||
// If you want to embed from a point to the end you should use:
|
||||
//
|
||||
// [embedmd]:# (pathOrURL language /start regexp/ $)
|
||||
//
|
||||
// Finally you can embed a whole file by omitting both regular expressions:
|
||||
//
|
||||
// [embedmd]:# (pathOrURL language)
|
||||
//
|
||||
// You can ommit the language in any of the previous commands, and the extension
|
||||
// of the file will be used for the snippet syntax highlighting. Note that while
|
||||
// this works Go files, since the file extension .go matches the name of the language
|
||||
// go, this will fail with other files like .md whose language name is markdown.
|
||||
//
|
||||
// [embedmd]:# (file.ext)
|
||||
//
|
||||
package embedmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Process reads markdown from the given io.Reader searching for an embedmd
|
||||
// command. When a command is found, it is executed and the output is written
|
||||
// into the given io.Writer with the rest of standard markdown.
|
||||
func Process(out io.Writer, in io.Reader, opts ...Option) error {
|
||||
e := embedder{Fetcher: fetcher{}}
|
||||
for _, opt := range opts {
|
||||
opt.f(&e)
|
||||
}
|
||||
return process(out, in, e.runCommand)
|
||||
}
|
||||
|
||||
// An Option provides a way to adapt the Process function to your needs.
|
||||
type Option struct{ f func(*embedder) }
|
||||
|
||||
// WithBaseDir indicates that the given path should be used to resolve relative
|
||||
// paths.
|
||||
func WithBaseDir(path string) Option {
|
||||
return Option{func(e *embedder) { e.baseDir = path }}
|
||||
}
|
||||
|
||||
// WithFetcher provides a custom Fetcher to be used whenever a path or url needs
|
||||
// to be fetched.
|
||||
func WithFetcher(c Fetcher) Option {
|
||||
return Option{func(e *embedder) { e.Fetcher = c }}
|
||||
}
|
||||
|
||||
type embedder struct {
|
||||
Fetcher
|
||||
baseDir string
|
||||
}
|
||||
|
||||
func (e *embedder) runCommand(w io.Writer, cmd *command) error {
|
||||
b, err := e.Fetch(e.baseDir, cmd.path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read %s: %v", cmd.path, err)
|
||||
}
|
||||
|
||||
b, err = extract(b, cmd.start, cmd.end)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not extract content from %s: %v", cmd.path, err)
|
||||
}
|
||||
|
||||
if len(b) > 0 && b[len(b)-1] != '\n' {
|
||||
b = append(b, '\n')
|
||||
}
|
||||
|
||||
fmt.Fprintln(w, "```"+cmd.lang)
|
||||
w.Write(b)
|
||||
fmt.Fprintln(w, "```")
|
||||
return nil
|
||||
}
|
||||
|
||||
func extract(b []byte, start, end *string) ([]byte, error) {
|
||||
if start == nil && end == nil {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
match := func(s string) ([]int, error) {
|
||||
if len(s) <= 2 || s[0] != '/' || s[len(s)-1] != '/' {
|
||||
return nil, fmt.Errorf("missing slashes (/) around %q", s)
|
||||
}
|
||||
re, err := regexp.CompilePOSIX(s[1 : len(s)-1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
loc := re.FindIndex(b)
|
||||
if loc == nil {
|
||||
return nil, fmt.Errorf("could not match %q", s)
|
||||
}
|
||||
return loc, nil
|
||||
}
|
||||
|
||||
if *start != "" {
|
||||
loc, err := match(*start)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if end == nil {
|
||||
return b[loc[0]:loc[1]], nil
|
||||
}
|
||||
b = b[loc[0]:]
|
||||
}
|
||||
|
||||
if *end != "$" {
|
||||
loc, err := match(*end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = b[:loc[1]]
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
117
vendor/github.com/campoy/embedmd/embedmd/parser.go
generated
vendored
Normal file
117
vendor/github.com/campoy/embedmd/embedmd/parser.go
generated
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2016 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package embedmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type commandRunner func(io.Writer, *command) error
|
||||
|
||||
func process(out io.Writer, in io.Reader, run commandRunner) error {
|
||||
s := &countingScanner{bufio.NewScanner(in), 0}
|
||||
|
||||
state := parsingText
|
||||
var err error
|
||||
for state != nil {
|
||||
state, err = state(out, s, run)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%d: %v", s.line, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.Err(); err != nil {
|
||||
return fmt.Errorf("%d: %v", s.line, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type countingScanner struct {
|
||||
*bufio.Scanner
|
||||
line int
|
||||
}
|
||||
|
||||
func (c *countingScanner) Scan() bool {
|
||||
b := c.Scanner.Scan()
|
||||
if b {
|
||||
c.line++
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
type textScanner interface {
|
||||
Text() string
|
||||
Scan() bool
|
||||
}
|
||||
|
||||
type state func(io.Writer, textScanner, commandRunner) (state, error)
|
||||
|
||||
func parsingText(out io.Writer, s textScanner, run commandRunner) (state, error) {
|
||||
if !s.Scan() {
|
||||
return nil, nil // end of file, which is fine.
|
||||
}
|
||||
switch line := s.Text(); {
|
||||
case strings.HasPrefix(line, "[embedmd]:#"):
|
||||
return parsingCmd, nil
|
||||
case strings.HasPrefix(line, "```"):
|
||||
return codeParser{print: true}.parse, nil
|
||||
default:
|
||||
fmt.Fprintln(out, s.Text())
|
||||
return parsingText, nil
|
||||
}
|
||||
}
|
||||
|
||||
func parsingCmd(out io.Writer, s textScanner, run commandRunner) (state, error) {
|
||||
line := s.Text()
|
||||
fmt.Fprintln(out, line)
|
||||
args := line[strings.Index(line, "#")+1:]
|
||||
cmd, err := parseCommand(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := run(out, cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !s.Scan() {
|
||||
return nil, nil // end of file, which is fine.
|
||||
}
|
||||
if strings.HasPrefix(s.Text(), "```") {
|
||||
return codeParser{print: false}.parse, nil
|
||||
}
|
||||
fmt.Fprintln(out, s.Text())
|
||||
return parsingText, nil
|
||||
}
|
||||
|
||||
type codeParser struct{ print bool }
|
||||
|
||||
func (c codeParser) parse(out io.Writer, s textScanner, run commandRunner) (state, error) {
|
||||
if c.print {
|
||||
fmt.Fprintln(out, s.Text())
|
||||
}
|
||||
if !s.Scan() {
|
||||
return nil, fmt.Errorf("unbalanced code section")
|
||||
}
|
||||
if !strings.HasPrefix(s.Text(), "```") {
|
||||
return c.parse, nil
|
||||
}
|
||||
|
||||
// print the end of the code section if needed and go back to parsing text.
|
||||
if c.print {
|
||||
fmt.Fprintln(out, s.Text())
|
||||
}
|
||||
return parsingText, nil
|
||||
}
|
3
vendor/github.com/campoy/embedmd/go.mod
generated
vendored
Normal file
3
vendor/github.com/campoy/embedmd/go.mod
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
module github.com/campoy/embedmd
|
||||
|
||||
require github.com/pmezard/go-difflib v1.0.0
|
2
vendor/github.com/campoy/embedmd/go.sum
generated
vendored
Normal file
2
vendor/github.com/campoy/embedmd/go.sum
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
185
vendor/github.com/campoy/embedmd/main.go
generated
vendored
Normal file
185
vendor/github.com/campoy/embedmd/main.go
generated
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
// Copyright 2016 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// embedmd
|
||||
//
|
||||
// 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 none is given it
|
||||
// reads from the standard input.
|
||||
//
|
||||
// embedmd supports two flags:
|
||||
// -d: will print the difference of the input file with what the output
|
||||
// would have been if executed.
|
||||
// -w: rewrites the given files rather than writing the output to the standard
|
||||
// output.
|
||||
//
|
||||
// For more information on the format of the commands, read the documentation
|
||||
// of the github.com/campoy/embedmd/embedmd package.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/campoy/embedmd/embedmd"
|
||||
"github.com/pmezard/go-difflib/difflib"
|
||||
)
|
||||
|
||||
// modified while building by -ldflags.
|
||||
var version = "unkown"
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, "usage: embedmd [flags] [path ...]\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
func main() {
|
||||
rewrite := flag.Bool("w", false, "write result to (markdown) file instead of stdout")
|
||||
doDiff := flag.Bool("d", false, "display diffs instead of rewriting files")
|
||||
printVersion := flag.Bool("v", false, "display embedmd version")
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
||||
if *printVersion {
|
||||
fmt.Println("embedmd version: " + version)
|
||||
return
|
||||
}
|
||||
|
||||
diff, err := embed(flag.Args(), *rewrite, *doDiff)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
if diff && *doDiff {
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
stdout io.Writer = os.Stdout
|
||||
stdin io.Reader = os.Stdin
|
||||
)
|
||||
|
||||
func embed(paths []string, rewrite, doDiff bool) (foundDiff bool, err error) {
|
||||
if rewrite && doDiff {
|
||||
return false, fmt.Errorf("error: cannot use -w and -d simultaneously")
|
||||
}
|
||||
|
||||
if len(paths) == 0 {
|
||||
if rewrite {
|
||||
return false, fmt.Errorf("error: cannot use -w with standard input")
|
||||
}
|
||||
if !doDiff {
|
||||
return false, embedmd.Process(stdout, stdin)
|
||||
}
|
||||
|
||||
var out, in bytes.Buffer
|
||||
if err := embedmd.Process(&out, io.TeeReader(stdin, &in)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
d, err := diff(in.String(), out.String())
|
||||
if err != nil || len(d) == 0 {
|
||||
return false, err
|
||||
}
|
||||
fmt.Fprintf(stdout, "%s", d)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
d, err := processFile(path, rewrite, doDiff)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s:%v", path, err)
|
||||
}
|
||||
foundDiff = foundDiff || d
|
||||
}
|
||||
return foundDiff, nil
|
||||
}
|
||||
|
||||
type file interface {
|
||||
io.ReadCloser
|
||||
io.WriterAt
|
||||
Truncate(int64) error
|
||||
}
|
||||
|
||||
// replaced by testing functions.
|
||||
var openFile = func(name string) (file, error) {
|
||||
return os.OpenFile(name, os.O_RDWR, 0666)
|
||||
}
|
||||
|
||||
func readFile(path string) ([]byte, error) {
|
||||
f, err := openFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return ioutil.ReadAll(f)
|
||||
}
|
||||
|
||||
func processFile(path string, rewrite, doDiff bool) (foundDiff bool, err error) {
|
||||
if filepath.Ext(path) != ".md" {
|
||||
return false, fmt.Errorf("not a markdown file")
|
||||
}
|
||||
|
||||
f, err := openFile(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := embedmd.Process(buf, f, embedmd.WithBaseDir(filepath.Dir(path))); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if doDiff {
|
||||
f, err := readFile(path)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("could not read %s for diff: %v", path, err)
|
||||
}
|
||||
data, err := diff(string(f), buf.String())
|
||||
if err != nil || len(data) == 0 {
|
||||
return false, err
|
||||
}
|
||||
fmt.Fprintf(stdout, "%s", data)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if rewrite {
|
||||
n, err := f.WriteAt(buf.Bytes(), 0)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("could not write: %v", err)
|
||||
}
|
||||
return false, f.Truncate(int64(n))
|
||||
}
|
||||
|
||||
io.Copy(stdout, buf)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func diff(a, b string) (string, error) {
|
||||
return difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
|
||||
A: difflib.SplitLines(a),
|
||||
B: difflib.SplitLines(b),
|
||||
Context: 3,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user