diff --git a/mdoc/src/main/scala/mdoc/MainSettings.scala b/mdoc/src/main/scala/mdoc/MainSettings.scala index 13b952329..540f069ee 100644 --- a/mdoc/src/main/scala/mdoc/MainSettings.scala +++ b/mdoc/src/main/scala/mdoc/MainSettings.scala @@ -27,10 +27,10 @@ final class MainSettings private ( } } def withExcludePath(excludePath: List[PathMatcher]): MainSettings = { - copy(settings.copy(excludePath = excludePath)) + copy(settings.copy(exclude = excludePath)) } def withIncludePath(includePath: List[PathMatcher]): MainSettings = { - copy(settings.copy(includePath = includePath)) + copy(settings.copy(include = includePath)) } def withSiteVariables(variables: Map[String, String]): MainSettings = { copy(settings.copy(site = variables)) diff --git a/mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala b/mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala index 1adc5b934..3b02b5889 100644 --- a/mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala +++ b/mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala @@ -61,7 +61,7 @@ final class MainOps( def handleFile(file: InputFile): Exit = { try { - if (!settings.matches(file.relpath)) Exit.success + if (!settings.isIncluded(file.relpath)) Exit.success else { PathIO.extension(file.in.toNIO) match { case "md" => handleMarkdown(file) diff --git a/mdoc/src/main/scala/mdoc/internal/cli/Settings.scala b/mdoc/src/main/scala/mdoc/internal/cli/Settings.scala index b78e345ea..4e1a61fe2 100644 --- a/mdoc/src/main/scala/mdoc/internal/cli/Settings.scala +++ b/mdoc/src/main/scala/mdoc/internal/cli/Settings.scala @@ -73,10 +73,19 @@ case class Settings( usage: Boolean = false, @Description("Print out the version number and exit") version: Boolean = false, - @Description("Glob to filter which files from --in directory to include.") - includePath: List[PathMatcher] = Nil, - @Description("Glob to filter which files from --in directory to exclude.") - excludePath: List[PathMatcher] = Nil, + @Description( + "Glob to filter which files to process. Defaults to all files. " + + "Example: --include **/example.md will process only files with the name example.md." + ) + @ExtraName("includePath") + include: List[PathMatcher] = Nil, + @Description( + "Glob to filter which files from exclude from processing. Defaults to no files. " + + "Example: --include users/**.md --exclude **/example.md will process all files in the users/ directory " + + "excluding files named example.md." + ) + @ExtraName("excludePath") + exclude: List[PathMatcher] = Nil, @Description( "Use relative filenames when reporting error messages. " + "Useful for producing consistent docs on a local machine and CI. " @@ -99,16 +108,19 @@ case class Settings( def toInputFile(infile: AbsolutePath): Option[InputFile] = { val relpath = infile.toRelative(in) - if (matches(relpath)) { + if (isIncluded(relpath)) { val outfile = out.resolve(relpath) Some(InputFile(relpath, infile, outfile)) } else { None } } - def matches(path: RelativePath): Boolean = { - (includePath.isEmpty || includePath.exists(_.matches(path.toNIO))) && - !excludePath.exists(_.matches(path.toNIO)) + def isExplicitlyExcluded(path: RelativePath): Boolean = { + exclude.exists(_.matches(path.toNIO)) + } + def isIncluded(path: RelativePath): Boolean = { + (include.isEmpty || include.exists(_.matches(path.toNIO))) && + !isExplicitlyExcluded(path) } def validate(logger: Reporter): Configured[Context] = { if (Files.exists(in.toNIO)) { @@ -145,7 +157,8 @@ object Settings extends MetaconfigScalametaImplicits { |Example: mdoc --in --out (customize input/output directories) | mdoc --watch (watch for file changes) | mdoc --site.VERSION 1.0.0 (pass in site variables) - | mdoc --exclude-path (exclude files matching patterns) + | mdoc --include **/example.md (process only files named example.md) + | mdoc --exclude node_modules (don't process node_modules directory) |""".stripMargin def description: Doc = Doc.paragraph( diff --git a/mdoc/src/main/scala/mdoc/internal/io/IO.scala b/mdoc/src/main/scala/mdoc/internal/io/IO.scala index 38c411641..ad783be27 100644 --- a/mdoc/src/main/scala/mdoc/internal/io/IO.scala +++ b/mdoc/src/main/scala/mdoc/internal/io/IO.scala @@ -32,8 +32,8 @@ object IO { override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult = { val relpath = RelativePath(root.relativize(dir)) - if (dir == root || settings.matches(relpath)) FileVisitResult.CONTINUE - else FileVisitResult.SKIP_SUBTREE // excluded + if (settings.isExplicitlyExcluded(relpath)) FileVisitResult.SKIP_SUBTREE + else FileVisitResult.CONTINUE } } Files.walkFileTree(root, visitor) diff --git a/readme.md b/readme.md index fa7430dac..d246cd2c3 100644 --- a/readme.md +++ b/readme.md @@ -711,7 +711,8 @@ Usage: mdoc [