/* @flow */

import Rule from '../Rule'

import type { CommandOptions } from '../types'

const PDF_CAPABLE_LATEX_PATTERN = /^(pdf|xe|lua)latex$/

export default class Pweave extends Rule {
  static parameterTypes: Array<Set<string>> = [new Set(['PythonNoWeb'])]
  static description: string = 'Runs Pweave on Pnw files.'

  constructCommand (): CommandOptions {
    const cacheDirectory = this.options.pweaveCacheDirectory
    const figureDirectory = this.options.pweaveFigureDirectory
    const outputPath = this.options.pweaveOutputPath
    const args = [
      'pweave',
      `--format=${this.options.pweaveOutputFormat}`,
      `--output={{${outputPath}}}`
    ]
    let figureFormat = this.options.pweaveFigureFormat

    if (cacheDirectory !== 'cache') {
      args.push(`--cache-directory={{${cacheDirectory}}}`)
    }

    if (figureDirectory !== 'figures') {
      args.push(`--figure-directory={{${figureDirectory}}}`)
    }

    if (this.options.pweaveDocumentationMode) {
      args.push('--documentation-mode')
    }

    if (figureFormat === 'auto') {
      figureFormat = (this.options.outputFormat === 'pdf' && PDF_CAPABLE_LATEX_PATTERN.test(this.options.engine))
        ? 'pdf'
        : 'eps'
    }

    if (figureFormat !== 'pdf') {
      args.push(`--figure-format=${figureFormat}`)
    }

    args.push('{{$FILEPATH_0}}')

    return {
      args,
      cd: '$ROOTDIR',
      severity: 'error',
      outputs: [outputPath]
    }
  }
}

@@ -532,16 +532,65 @@
 putPath%0A
+    // Always add output format and output path.%0A
     cons
@@ -600,16 +600,16 @@
 rgs = %5B%0A
-
       'p
@@ -766,16 +766,78 @@
 Format%0A%0A
+    // If the cache directory is not the default then set it.%0A
     if (
@@ -924,32 +924,95 @@
 ory%7D%7D%7D%60)%0A    %7D%0A%0A
+    // If the figure directory is not the default then set it.%0A
     if (figureDi
@@ -1096,32 +1096,87 @@
 ory%7D%7D%7D%60)%0A    %7D%0A%0A
+    // Add the documentation mode option if it is set.%0A
     if (this.opt
@@ -1246,32 +1246,200 @@
 n-mode')%0A    %7D%0A%0A
+    // If the output format is pdf the set the output format to pdf unless%0A    // the current LaTeX engine cannot produce pdf files. In that case use eps%0A    // files.%0A
     if (figureFo
@@ -1606,24 +1606,68 @@
 eps'%0A    %7D%0A%0A
+    // Only set the option if it is not pdf%0A
     if (figu

Add comments