Skip to content

Commit

Permalink
Merge 8669fb9 into 58cfb43
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnthe committed Apr 28, 2018
2 parents 58cfb43 + 8669fb9 commit 796b82c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions __tests__/fixture.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

Consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa.

include::{var1}/{var2}/bar.adoc[]
28 changes: 28 additions & 0 deletions __tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,33 @@ Invalid regular expression: /(/: Unterminated group`);
expect(ret.stdout).not.toMatch('Consectetuer');
expect(ret.stdout).toMatch('Aenean massa.');
});
it('fails for invalid attribute', () => {
const ret = run([
'gettextize', '-m', fixture,
'-a', 'var1',
]);
expect(ret.status).toEqual(1);
expect(ret.stderr).toMatch('Error in --attribute');
});
it('fails for missing attribute name', () => {
const ret = run([
'gettextize', '-m', fixture,
'-a', '=value',
]);
expect(ret.status).toEqual(1);
expect(ret.stderr).toMatch('Error in --attribute');
});
it('can pass variables', () => {
const ret = run([
'gettextize', '-m', fixture,
'-a', 'var1=abc',
'--attribute', 'var2=xyz',
]);
expect(ret.status).toEqual(0);
// The fixture contains an include directive that contains attributes.
// If the attributes are not properly set, a "Unresolved directive ..."
// message is emitted in the output.
expect(ret.stdout).not.toMatch('Unresolved directive');
});
});
});
26 changes: 25 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { allBuiltinsAttributeFilter, extract } from './extract';
import { po } from 'gettext-parser';
import { translationObjectFromExtractions } from './adapter';
import { writeFileSync } from 'fs';
import Attributes = AsciiDoctorJs.Attributes;

function getBlacklistRegexes(program: Command) {
const blacklistRegexes = (program.ignore as string[]).map((pattern) => {
Expand All @@ -21,13 +22,34 @@ function getBlacklistRegexes(program: Command) {
return blacklistRegexes;
}

function getAttributes(program: Command): Attributes {
return (program.attribute as string[]).reduce((attributes: Attributes, arg) => {
const splitArg = arg.split('=');
if (splitArg.length !== 2) {
process.stderr.write(
`Error in --attribute "${arg}", format must be "name=value"`);
process.exit(1);
}
const [name, value] = splitArg;
if (name.length === 0) {
process.stderr.write(
`Error in --attribute "${arg}", missing name`);
process.exit(1);
}
attributes[name] = value;
return attributes;
}, {});
}

export function gettextizeAction(program: Command) {
if (!program.master) {
program.help();
return;
}

const document = asciidoctor.loadFile(program.master, {});
const document = asciidoctor.loadFile(program.master, {
attributes: getAttributes(program),
});
const extractions = extract(document, {
attributeFilter: program.builtinAttrs ? undefined : allBuiltinsAttributeFilter,
});
Expand Down Expand Up @@ -58,6 +80,8 @@ export function cli(argv: string[], stdout: WriteStream= process.stdout) {
program
.command('gettextize')
.description('Extracts texts from asciidoc file and generates a .pot file')
.option('-a, --attribute <name=value>',
'Define an attribute.', collect, [])
.option('-m, --master <path>',
'File containing the master document to translate.')
.option('-p, --po <path>',
Expand Down

0 comments on commit 796b82c

Please sign in to comment.