diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2017-09-22 23:58:59 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-09-22 23:58:59 -0600 |
| commit | 9a50c096621dfecfa0f926cf6b3e5a87f864b48b (patch) | |
| tree | 16db7b94845a8382fd8d873edcb6aed1b0ac795d | |
| parent | 129024b0d1ce1d4a6ed2087fb687429f017444a4 (diff) | |
resolves #1394 preserve directories if source dir and destination dir are set (PR #2421)
| -rw-r--r-- | lib/asciidoctor/cli/invoker.rb | 18 | ||||
| -rw-r--r-- | lib/asciidoctor/cli/options.rb | 4 | ||||
| -rw-r--r-- | man/asciidoctor.1 | 29 | ||||
| -rw-r--r-- | man/asciidoctor.adoc | 6 | ||||
| -rw-r--r-- | test/fixtures/subdir/index.adoc | 3 | ||||
| -rw-r--r-- | test/invoker_test.rb | 18 |
6 files changed, 72 insertions, 6 deletions
diff --git a/lib/asciidoctor/cli/invoker.rb b/lib/asciidoctor/cli/invoker.rb index c8049e29..139585d6 100644 --- a/lib/asciidoctor/cli/invoker.rb +++ b/lib/asciidoctor/cli/invoker.rb @@ -36,6 +36,8 @@ module Asciidoctor opts = {} infiles = [] outfile = nil + abs_srcdir_posix = nil + non_posix_env = ::File::ALT_SEPARATOR == '\\' err = @err || $stderr show_timings = false @@ -45,6 +47,11 @@ module Asciidoctor infiles = val when :output_file outfile = val + when :source_dir + if val + abs_srcdir_posix = ::File.expand_path val + abs_srcdir_posix = abs_srcdir_posix.tr '\\', '/' if non_posix_env && (abs_srcdir_posix.include? '\\') + end when :destination_dir opts[:to_dir] = val if val when :attributes @@ -100,6 +107,17 @@ module Asciidoctor else infiles.each do |infile| input_opts = opts.merge :to_file => tofile + if abs_srcdir_posix && (input_opts.key? :to_dir) + abs_indir = ::File.dirname ::File.expand_path infile + if non_posix_env + abs_indir_posix = (abs_indir.include? '\\') ? (abs_indir.tr '\\', '/') : abs_indir + else + abs_indir_posix = abs_indir + end + if abs_indir_posix.start_with? %(#{abs_srcdir_posix}/) + input_opts[:to_dir] += abs_indir.slice abs_srcdir_posix.length, abs_indir.length + end + end if show_timings @documents << (::Asciidoctor.convert_file infile, (input_opts.merge :timings => (timings = Timings.new))) timings.print_report err, infile diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb index 8928ed15..b2fa4d65 100644 --- a/lib/asciidoctor/cli/options.rb +++ b/lib/asciidoctor/cli/options.rb @@ -24,6 +24,7 @@ module Asciidoctor self[:load_paths] = options[:load_paths] || nil self[:requires] = options[:requires] || nil self[:base_dir] = options[:base_dir] + self[:source_dir] = options[:source_dir] || nil self[:destination_dir] = options[:destination_dir] || nil self[:trace] = false self[:timings] = false @@ -104,6 +105,9 @@ Example: asciidoctor -b html5 source.asciidoc opts.on('-B', '--base-dir DIR', 'base directory containing the document and resources (default: directory of source file)') do |base_dir| self[:base_dir] = base_dir end + opts.on('-R', '--source-dir DIR', 'source root directory (used for calculating path in destination directory)') do |src_dir| + self[:source_dir] = src_dir + end opts.on('-D', '--destination-dir DIR', 'destination output directory (default: directory of source file)') do |dest_dir| self[:destination_dir] = dest_dir end diff --git a/man/asciidoctor.1 b/man/asciidoctor.1 index 3fcf67a1..b2b0e841 100644 --- a/man/asciidoctor.1 +++ b/man/asciidoctor.1 @@ -1,23 +1,32 @@ '\" t .\" Title: asciidoctor .\" Author: Dan Allen, Sarah White, Ryan Waldron -.\" Generator: Asciidoctor 1.5.6.1 -.\" Date: 2017-07-23 +.\" Generator: Asciidoctor 1.5.7.dev +.\" Date: 2017-09-22 .\" Manual: Asciidoctor Manual .\" Source: Asciidoctor 1.5.6.1 .\" Language: English .\" -.TH "ASCIIDOCTOR" "1" "2017-07-23" "Asciidoctor 1.5.6.1" "Asciidoctor Manual" +.TH "ASCIIDOCTOR" "1" "2017-09-22" "Asciidoctor 1.5.6.1" "Asciidoctor Manual" .ie \n(.g .ds Aq \(aq .el .ds Aq ' .ss \n[.ss] 0 .nh .ad l .de URL -\\$2 \(laURL: \\$1 \(ra\\$3 +\fI\\$2\fP <\\$1>\\$3 .. -.if \n[.g] .mso www.tmac -.LINKSTYLE blue R < > +.als MTO URL +.if \n[.g] \{\ +. mso www.tmac +. am URL +. ad l +. . +. am MTO +. ad l +. . +. LINKSTYLE blue R < > +.\} .SH "NAME" asciidoctor \- converts AsciiDoc source files to HTML, DocBook and other formats .SH "SYNOPSIS" @@ -134,6 +143,14 @@ If the input is read from standard input or a named pipe (fifo), then the output If \fIOUT_FILE\fP is \fI\-\fP, then the output file is written to standard output. .RE .sp +\fB\-R, \-\-source\-dir\fP=\fIDIR\fP +.RS 4 +Source directory. +Currently only used if the destination directory is also specified. +Used to preserve the directory structure of files converted within this directory in the destination directory. +If specified, the directory is resolved relative to the working directory. +.RE +.sp \fB\-r, \-\-require\fP=\fILIBRARY\fP .RS 4 Require the specified library before executing the processor, using the standard Ruby require. diff --git a/man/asciidoctor.adoc b/man/asciidoctor.adoc index 3a49593a..2bd19e00 100644 --- a/man/asciidoctor.adoc +++ b/man/asciidoctor.adoc @@ -102,6 +102,12 @@ This option may be specified more than once. If the input is read from standard input or a named pipe (fifo), then the output file defaults to stdout. If _OUT_FILE_ is _-_, then the output file is written to standard output. +*-R, --source-dir*=_DIR_:: + Source directory. + Currently only used if the destination directory is also specified. + Used to preserve the directory structure of files converted within this directory in the destination directory. + If specified, the directory is resolved relative to the working directory. + *-r, --require*=_LIBRARY_:: Require the specified library before executing the processor, using the standard Ruby require. This option may be specified more than once. diff --git a/test/fixtures/subdir/index.adoc b/test/fixtures/subdir/index.adoc new file mode 100644 index 00000000..1bf7957b --- /dev/null +++ b/test/fixtures/subdir/index.adoc @@ -0,0 +1,3 @@ += Sample Document + +content diff --git a/test/invoker_test.rb b/test/invoker_test.rb index fd559168..aa7719c9 100644 --- a/test/invoker_test.rb +++ b/test/invoker_test.rb @@ -238,6 +238,24 @@ context 'Invoker' do end end + test 'should preserve directory structure in destination directory if source directory is set' do + sample_inpath = 'subdir/index.adoc' + destination_path = 'test_output' + destination_subdir_path = File.join destination_path, 'subdir' + sample_outpath = File.join destination_subdir_path, 'index.html' + begin + FileUtils.mkdir_p(destination_path) + invoker = invoke_cli %W(-D #{destination_path} -R test/fixtures), sample_inpath + doc = invoker.document + assert File.directory?(destination_subdir_path) + assert File.exist?(sample_outpath) + ensure + FileUtils.rm_f(sample_outpath) + FileUtils.rmdir(destination_subdir_path) + FileUtils.rmdir(destination_path) + end + end + test 'should output to file specified' do sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample-output.html')) begin |
