feat(xmb/xtb): support dtd

This commit is contained in:
Victor Berchet
2016-07-29 13:07:01 -07:00
parent 44093905e2
commit e34a04d2ad
5 changed files with 102 additions and 19 deletions

View File

@ -18,11 +18,29 @@ const _MESSAGE_TAG = 'msg';
const _PLACEHOLDER_TAG = 'ph';
const _EXEMPLE_TAG = 'ex';
const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
<!ATTLIST messagebundle class CDATA #IMPLIED>
<!ELEMENT msg (#PCDATA|ph|source)*>
<!ATTLIST msg id CDATA #IMPLIED>
<!ATTLIST msg seq CDATA #IMPLIED>
<!ATTLIST msg name CDATA #IMPLIED>
<!ATTLIST msg desc CDATA #IMPLIED>
<!ATTLIST msg meaning CDATA #IMPLIED>
<!ATTLIST msg obsolete (obsolete) #IMPLIED>
<!ATTLIST msg xml:space (default|preserve) "default">
<!ATTLIST msg is_hidden CDATA #IMPLIED>
<!ELEMENT source (#PCDATA)>
<!ELEMENT ph (#PCDATA|ex)*>
<!ATTLIST ph name CDATA #REQUIRED>
<!ELEMENT ex (#PCDATA)>`;
export class Xmb implements Serializer {
// TODO(vicb): DOCTYPE
write(messageMap: {[k: string]: i18n.Message}): string {
const visitor = new _Visitor();
const declaration = new xml.Declaration({version: '1.0', encoding: 'UTF-8'});
let rootNode = new xml.Tag(_MESSAGES_TAG);
rootNode.children.push(new xml.Text('\n'));
@ -44,7 +62,9 @@ export class Xmb implements Serializer {
});
return xml.serialize([
declaration,
new xml.Declaration({version: '1.0', encoding: 'UTF-8'}),
new xml.Text('\n'),
new xml.Doctype(_MESSAGES_TAG, _DOCTYPE),
new xml.Text('\n'),
rootNode,
]);

View File

@ -10,6 +10,7 @@ export interface IVisitor {
visitTag(tag: Tag): any;
visitText(text: Text): any;
visitDeclaration(decl: Declaration): any;
visitDoctype(doctype: Doctype): any;
}
class _Visitor implements IVisitor {
@ -36,6 +37,10 @@ class _Visitor implements IVisitor {
.join(' ');
return strAttrs.length > 0 ? ' ' + strAttrs : '';
}
visitDoctype(doctype: Doctype): any {
return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
}
}
const _visitor = new _Visitor();
@ -58,6 +63,12 @@ export class Declaration implements Node {
visit(visitor: IVisitor): any { return visitor.visitDeclaration(this); }
}
export class Doctype implements Node {
constructor(public rootTag: string, public dtd: string){};
visit(visitor: IVisitor): any { return visitor.visitDoctype(this); }
}
export class Tag implements Node {
public attrs: {[k: string]: string} = {};