Skip to content

Commit

Permalink
Add option to format files directly from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed Feb 10, 2019
1 parent 68bfbb3 commit b56c949
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/adapters/troff/groff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export default class GroffAdapter {
* @param {String} [options.defaultFont] - Default font family
* @param {Number} [options.defaultStroke] - Default stroke width
* @param {String} [options.fontDescPath] - Path of device DESC files
* @param {String} [options.inputFile] - Format the given file, prepending any given input
* @param {Boolean} [options.landscape] - Print landscape pages for devices which support it
* @param {String} [options.macroFilePath] - Search path for macro files
* @param {String[]} [options.macros] - List of macro packages to include
Expand All @@ -211,6 +212,7 @@ export default class GroffAdapter {
*/
async format(input, device, options = {}){
const features = this.getDeviceFeatures(device);
input = String(input || "");

// Collect transparent options which can be bundled
let args = "";
Expand Down Expand Up @@ -255,6 +257,17 @@ export default class GroffAdapter {
options.args && args.push(...options.args);
args.push("-T" + device);

// Resolve input source
let {inputFile} = options;
if(inputFile && input){
input += await readFile(inputFile);
inputFile = "";
}
if(inputFile){
input = null;
args.push(inputFile);
}

const outputEncoding = options.raw ? "utf8" : features.has("encoding") ? device : "binary";
const result = await exec(this.path, args.filter(Boolean), input, {
encoding: ["utf8", outputEncoding, "utf8"],
Expand Down
10 changes: 10 additions & 0 deletions test/2.3-groff.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ describe("GroffAdapter", function(){
expect(await groff.format("Foo", "utf8", {args: ["-Z"], fontDescPath})).to.match(/^x res 640 32 48$/m);
});

it("supports direct file reads", async () => {
const inputFile = join(__dirname, "fixtures", "troff", "format.me");
expect(await groff.format("", "utf8", {inputFile})).to.match(/^Hello, world\s*$/);
expect(await groff.format(null, "utf8", {inputFile})).to.match(/^Hello, world\s*$/);
expect(await groff.format(null, "utf8", {inputFile, pageWidth: "5n"})).to.match(/^Hello,\nworld\s*$/);
const inputData = ".ds str Goodbye, world\n";
expect(await groff.format(inputData, "utf8", {inputFile})).to.match(/^Goodbye, world\s*$/);
expect(await groff.format(inputData, "utf8", {inputFile, pageWidth: "5n"})).to.match(/^Goodbye,\nworld\s*$/);
});

it("supports landscape orientation", async () => {
expect(await groff.format("Foo", "ps", {landscape: false})).to.match(/^%%Orientation: Portrait$/m);
expect(await groff.format("Foo", "ps", {landscape: true})).to.match(/^%%Orientation: Landscape$/m);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/troff/format.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nh
.if !d str .ds str Hello, world
\*[str]

0 comments on commit b56c949

Please sign in to comment.