Formatter move each formatter to separate file

This commit is contained in:
Michal Szczepanski 2019-07-23 00:17:48 +02:00
parent b638f75bca
commit c141efb7a6
4 changed files with 112 additions and 103 deletions

@ -1,3 +1,7 @@
const FormatterJSON = require('./formatter/FormatterJSON');
const FormatterXML = require('./formatter/FormatterXML');
const FormatterText = require('./formatter/FormatterText');
class Formatter {
constructor() {
@ -24,107 +28,4 @@ class Formatter {
}
}
class FormatterText {
start(doc, metadata) {
return ''
}
format(page, data, last) {
return ''
}
end() {
return ''
}
}
class FormatterJSON {
start(doc, metadata) {
const meta = JSON.stringify(metadata)
return `{
"pages_count": ${doc.numPages},
"metadata": ${meta},
"pages": {
`
}
formatTextObject(textObject) {
const txtObjOut = {lines: [], x: textObject.x, y: textObject.y};
textObject.getData().forEach(textLine => {
const txtLineOut = this.formatTextLine(textLine);
txtObjOut.lines.push(txtLineOut);
});
return txtObjOut;
}
formatTextLine(textLine) {
const txtLineOut = {
text: [],
x: textLine.x,
y: textLine.y,
w: textLine.w,
h: textLine.h,
textMatrix: textLine.textMatrix,
}
textLine.getData().forEach(textFont => {
const txtFontOut = this.formatTextFont(textFont);
txtLineOut.text.push(txtFontOut);
});
return txtLineOut;
}
formatTextFont(textFont) {
const font = textFont.getFont();
return {
font: {
size: font.size,
direction: font.direction,
family: font.family,
style: font.style,
weight: font.weight,
},
text: textFont.getText(),
charSpacing: textFont.charSpacing,
wordSpacing: textFont.wordSpacing,
}
}
format(page, data, last) {
const txtData = [];
data.forEach(textObject => {
const txtObjOut = this.formatTextObject(textObject);
txtData.push(txtObjOut);
});
let output = {
"data": txtData,
}
const out = JSON.stringify(output)// pretty print (output, null, 4)
return `"${page.pageIndex}": ${out}${last ? '': ','}`
}
end() {
return `}
}
`
}
}
class FormatterXML {
start(doc, metadata) {
return `<?xml version="1.0" encoding="UTF-8"?>
<document>
`
}
format(page, data) {
const output = '';
return output
}
end() {
return '</document>'
}
}
module.exports = Formatter;

@ -0,0 +1,72 @@
class FormatterJSON {
start(doc, metadata) {
const meta = JSON.stringify(metadata)
return `{
"pages_count": ${doc.numPages},
"metadata": ${meta},
"pages": {
`
}
formatTextObject(textObject) {
const txtObjOut = {lines: [], x: textObject.x, y: textObject.y};
textObject.getData().forEach(textLine => {
const txtLineOut = this.formatTextLine(textLine);
txtObjOut.lines.push(txtLineOut);
});
return txtObjOut;
}
formatTextLine(textLine) {
const txtLineOut = {
text: [],
x: textLine.x,
y: textLine.y,
w: textLine.w,
h: textLine.h,
textMatrix: textLine.textMatrix,
}
textLine.getData().forEach(textFont => {
const txtFontOut = this.formatTextFont(textFont);
txtLineOut.text.push(txtFontOut);
});
return txtLineOut;
}
formatTextFont(textFont) {
const font = textFont.getFont();
return {
font: {
size: font.size,
direction: font.direction,
family: font.family,
style: font.style,
weight: font.weight,
},
text: textFont.getText(),
charSpacing: textFont.charSpacing,
wordSpacing: textFont.wordSpacing,
}
}
format(page, data, last) {
const txtData = [];
data.forEach(textObject => {
const txtObjOut = this.formatTextObject(textObject);
txtData.push(txtObjOut);
});
let output = {
"data": txtData,
}
const out = JSON.stringify(output)// pretty print (output, null, 4)
return `"${page.pageIndex}": ${out}${last ? '': ','}`
}
end() {
return `}
}
`
}
}
module.exports = FormatterJSON;

@ -0,0 +1,17 @@
class FormatterText {
start(doc, metadata) {
return ''
}
format(page, data, last) {
return ''
}
end() {
return ''
}
}
module.exports = FormatterText;

@ -0,0 +1,19 @@
class FormatterXML {
start(doc, metadata) {
return `<?xml version="1.0" encoding="UTF-8"?>
<document>
`
}
format(page, data) {
const output = '';
return output
}
end() {
return '</document>'
}
}
module.exports = FormatterXML;