Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions EMBEDDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,13 @@ The fragments can also be used in other languages:
</body>
</html>
```

### Providing fragments directly

It is possible to add the desired fragment file with exactly the required content
directly to the fragments folder, for example: `FRAGMENTS/desired-path/MyFile.kt`.

It can then be embedded just like any other fragment file:
```markdown
<embed-code file="desired-path/MyFile.kt"></embed-code>
```
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,39 @@ embed-mappings:

The available fields for the configuration file are:
* `code-path`: (Mandatory) Path to the source code root.
May be represented as:
* single path
```yaml
code-path: path/to/code/root
```
* multiple named paths:
```yaml
code-path:
- name: examples
path: path/to/code/root1
- name: production
path: path/to/code/root2
```
When a named path is specified, fragments must be referenced in the embedding instructions
using the corresponding path name:
```md
<embed-code file="$PATH_NAME/path/to/file"></embed-code>
```
**Do not forget the dollar sign (`$`) before the path name.**

It is possible to specify a path without a name or with an empty name.
In this case, fragments will be stored in the root defined by `fragments-path`.

It is also possible to specify multiple paths with the same name,
but this may lead to fragments being overwritten if they have the same relative path and name.

* `docs-path`: (Mandatory) Path to the documentation root.
* `code-includes`: (Optional) Glob patterns for source files to include.
It may be represented as a comma-separated string list or as a YAML sequence.
* `doc-excludes`: (Optional) Glob patterns for documentation files to exclude.
It may be represented as a comma-separated string list or as a YAML sequence.
* `doc-includes`: (Optional) Glob patterns for documentation files to include.
It may be represented as a comma-separated string list or as a YAML sequence.
* `fragments-path`: (Optional) Directory for code fragments.
* `separator`: (Optional) Separator for fragments.
* `embed-mappings`: (Optional) A list of custom mappings, each containing `code-path` and `docs-path`.
Expand Down
Binary file modified bin/embed-code-linux
Binary file not shown.
Binary file modified bin/embed-code-macos
Binary file not shown.
Binary file modified bin/embed-code-windows.exe
Binary file not shown.
74 changes: 41 additions & 33 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package cli

import (
_type "embed-code/embed-code-go/type"
"flag"
"fmt"
"os"
Expand All @@ -34,54 +35,61 @@ import (

// Config — user-specified embed-code configurations.
//
// BaseCodePath — a path to a root directory with code files.
// BaseCodePaths — a NamedPathList to directories with code files.
//
// BaseDocsPath — a path to a root directory with docs files.
//
// CodeIncludes — a string with comma-separated patterns for filtering the code files
// CodeIncludes — a StringList with patterns for filtering the code files
// to be considered.
// Directories are never matched by these patterns.
// For example, "**/*.java,**/*.gradle".
// The default value is "**/*.*".
//
// DocIncludes — a string with comma-separated patterns for filtering files
// DocIncludes — a StringList with patterns for filtering files
// in which we should look for embedding instructions.
// The patterns are resolved relatively to the `documentation_root`.
// Directories are never matched by these patterns.
// For example, "docs/**/*.md,guides/*.html".
// The default value is "**/*.md,**/*.html".
//
// DocExcludes - a StringList with patterns for filtering documentation files
// which should be excluded from the embedding process.
//
// FragmentsPath — a directory where fragmented code is stored. A temporary directory that should
// not be tracked in VCS. The default value is: "./build/fragments".
//
// Separator — a string that's inserted between multiple partitions of a single fragment.
// The default value is "...".
//
// EmbedMappings — an additional optional list of configs, which will be executed together with the
// main one. A config written here has higher priority and may overwrite the base one.
//
// Info - specifies whether info-level logs should be shown.
//
// Stacktrace - specifies whether error stack traces should be shown.
//
// ConfigPath — a path to a yaml configuration file which contains the roots.
//
// Mode — defines the mode of embed-code execution.
//
// EmbedMappings — an additional optional list of configs, which will be executed together with the
// main one. A config written here has higher priority and may overwrite the base one.
type Config struct {
CodeIncludes string `yaml:"code-includes"`
DocIncludes string `yaml:"doc-includes"`
DocExcludes string `yaml:"doc-excludes"`
FragmentsPath string `yaml:"fragments-path"`
Separator string `yaml:"separator"`
BaseCodePath string `yaml:"code-path"`
BaseDocsPath string `yaml:"docs-path"`
EmbedMappings []EmbedMapping `yaml:"embed-mappings"`
Info bool `yaml:"info"`
Stacktrace bool `yaml:"stacktrace"`
BaseCodePaths _type.NamedPathList `yaml:"code-path"`
BaseDocsPath string `yaml:"docs-path"`
CodeIncludes _type.StringList `yaml:"code-includes"`
DocIncludes _type.StringList `yaml:"doc-includes"`
DocExcludes _type.StringList `yaml:"doc-excludes"`
FragmentsPath string `yaml:"fragments-path"`
Separator string `yaml:"separator"`
EmbedMappings []EmbedMapping `yaml:"embed-mappings"`
Info bool `yaml:"info"`
Stacktrace bool `yaml:"stacktrace"`
ConfigPath string
Mode string
}

// EmbedMapping is a pair of a source code path and a destination docs path to perform an embedding.
type EmbedMapping struct {
CodePath string `yaml:"code-path"`
DocsPath string `yaml:"docs-path"`
CodePath _type.NamedPathList `yaml:"code-path"`
DocsPath string `yaml:"docs-path"`
}

// EmbedCodeSamplesResult is result of the EmbedCodeSamples method.
Expand Down Expand Up @@ -158,11 +166,11 @@ func ReadArgs() Config {
flag.Parse()

return Config{
BaseCodePath: *codePath,
BaseCodePaths: _type.NamedPathList{_type.NamedPath{Path: *codePath}},
BaseDocsPath: *docsPath,
CodeIncludes: *codeIncludes,
DocIncludes: *docIncludes,
DocExcludes: *docExcludes,
CodeIncludes: parseListArgument(*codeIncludes),
DocIncludes: parseListArgument(*docIncludes),
DocExcludes: parseListArgument(*docExcludes),
FragmentsPath: *fragmentsPath,
Separator: *separator,
ConfigPath: *configPath,
Expand All @@ -180,18 +188,18 @@ func ReadArgs() Config {
func FillArgsFromConfigFile(args Config) (Config, error) {
configFields := readConfigFields(args.ConfigPath)
args.BaseDocsPath = configFields.BaseDocsPath
args.BaseCodePath = configFields.BaseCodePath
args.BaseCodePaths = configFields.BaseCodePaths

if isNotEmpty(configFields.CodeIncludes) {
if len(configFields.CodeIncludes) > 0 {
args.CodeIncludes = configFields.CodeIncludes
}
if len(configFields.EmbedMappings) > 0 {
args.EmbedMappings = configFields.EmbedMappings
}
if isNotEmpty(configFields.DocIncludes) {
if len(configFields.DocIncludes) > 0 {
args.DocIncludes = configFields.DocIncludes
}
if isNotEmpty(configFields.DocExcludes) {
if len(configFields.DocExcludes) > 0 {
args.DocExcludes = configFields.DocExcludes
}
if isNotEmpty(configFields.FragmentsPath) {
Expand All @@ -216,7 +224,7 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration
if len(userArgs.EmbedMappings) > 0 {
for _, mapping := range userArgs.EmbedMappings {
embedCodeConfig := configWithOptionalParams(userArgs)
embedCodeConfig.CodeRoot = mapping.CodePath
embedCodeConfig.CodeRoots = mapping.CodePath
embedCodeConfig.DocumentationRoot = mapping.DocsPath

// As the top config may overwrite those files, we need to exclude it from the embedding
Expand All @@ -226,10 +234,10 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration
}

embedCodeConfig := configWithOptionalParams(userArgs)
embedCodeConfig.CodeRoot = userArgs.BaseCodePath
embedCodeConfig.CodeRoots = userArgs.BaseCodePaths
embedCodeConfig.DocumentationRoot = userArgs.BaseDocsPath

if isNotEmpty(userArgs.DocExcludes) {
if len(userArgs.DocExcludes) > 0 {
embedCodeConfig.DocExcludes = append(embedCodeConfig.DocExcludes, excludedConfigs...)
} else {
embedCodeConfig.DocExcludes = excludedConfigs
Expand All @@ -243,11 +251,11 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration
func configWithOptionalParams(userArgs Config) configuration.Configuration {
embedCodeConfig := configuration.NewConfiguration()

if isNotEmpty(userArgs.CodeIncludes) {
embedCodeConfig.CodeIncludes = parseListArgument(userArgs.CodeIncludes)
if len(userArgs.CodeIncludes) > 0 {
embedCodeConfig.CodeIncludes = userArgs.CodeIncludes
}
if isNotEmpty(userArgs.DocIncludes) {
embedCodeConfig.DocIncludes = parseListArgument(userArgs.DocIncludes)
if len(userArgs.DocIncludes) > 0 {
embedCodeConfig.DocIncludes = userArgs.DocIncludes
}
if isNotEmpty(userArgs.FragmentsPath) {
embedCodeConfig.FragmentsDir = userArgs.FragmentsPath
Expand Down
9 changes: 5 additions & 4 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024, TeamDev. All rights reserved.
* Copyright 2026, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
Expand All @@ -22,6 +22,7 @@ package cli_test

import (
"embed-code/embed-code-go/cli"
_type "embed-code/embed-code-go/type"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -143,9 +144,9 @@ func baseCliConfig() cli.Config {
parentDir := filepath.Dir(currentDir)

return cli.Config{
Mode: cli.ModeCheck,
BaseDocsPath: parentDir + "/test/resources/docs",
BaseCodePath: parentDir + "/test/resources/code",
Mode: cli.ModeCheck,
BaseDocsPath: parentDir + "/test/resources/docs",
BaseCodePaths: _type.NamedPathList{_type.NamedPath{Path: parentDir + "/test/resources/code"}},
}
}

Expand Down
Loading