-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817d8c1
commit 4201645
Showing
10 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
import path from 'path'; | ||
import minimist from 'minimist'; | ||
import {fileURLToPath} from 'url'; | ||
import {Buffer} from 'buffer'; | ||
import fs from 'fs'; | ||
|
||
import {OdtProcessor} from '../odt/OdtProcessor.js'; | ||
import {UnMarshaller} from '../odt/UnMarshaller.js'; | ||
import {DocumentContent, DocumentStyles, LIBREOFFICE_CLASSES} from '../odt/LibreOffice.js'; | ||
import {OdtToMarkdown} from '../odt/OdtToMarkdown.js'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
process.env.GIT_SHA = process.env.GIT_SHA || 'dev'; | ||
|
||
async function usage() { | ||
const pkg = JSON.parse(new TextDecoder().decode(fs.readFileSync(path.resolve(__dirname, '..', '..', 'package.json')))); | ||
|
||
const commandUsage = 'echo "test" | odt2md\n\nor\n\nodt2md filename.odt'; | ||
|
||
console.log( | ||
`${pkg.name} version: ${pkg.version}, ${process.env.GIT_SHA}\n\nUsage:\n${commandUsage.trim()}\n`); | ||
} | ||
|
||
async function main() { | ||
const inputArr = []; | ||
|
||
process.stdin.on( 'data', function(data) { inputArr.push(data); } ); | ||
|
||
await new Promise(resolve => { | ||
setTimeout(() => { | ||
process.stdin.destroy(); | ||
resolve(null); | ||
}, 50); | ||
process.stdin.on( 'end', resolve); | ||
}); | ||
|
||
const argv = minimist(process.argv.slice(2)); | ||
|
||
if (inputArr.length === 0) { | ||
if (argv._.length < 1 || argv.h || argv.help) { | ||
await usage(); | ||
process.exit(1); | ||
} | ||
|
||
inputArr.push(fs.readFileSync(path.resolve(process.cwd(), argv._[0]))); | ||
} | ||
|
||
if (inputArr.length === 0) { | ||
console.error('No input'); | ||
process.exit(1); | ||
} | ||
|
||
const processor = new OdtProcessor(); | ||
await processor.loadFromBuffer(Buffer.concat(inputArr)); | ||
if (!processor.getContentXml()) { | ||
throw Error('No odt processed'); | ||
} | ||
|
||
const parser = new UnMarshaller(LIBREOFFICE_CLASSES, 'DocumentContent'); | ||
const document: DocumentContent = parser.unmarshal(processor.getContentXml()); | ||
if (!document) { | ||
throw Error('No document unmarshalled'); | ||
} | ||
const parserStyles = new UnMarshaller(LIBREOFFICE_CLASSES, 'DocumentStyles'); | ||
const styles: DocumentStyles = parserStyles.unmarshal(processor.getStylesXml()); | ||
if (!styles) { | ||
throw Error('No styles unmarshalled'); | ||
} | ||
const converter = new OdtToMarkdown(document, styles, processor.getFileNameMap(), processor.getXmlMap()); | ||
const markdown = await converter.convert(); | ||
console.log(markdown); | ||
} | ||
|
||
try { | ||
await main(); | ||
process.exit(0); | ||
} catch (err) { | ||
if (err.isUsageError) { | ||
console.error(err.message); | ||
await usage(); | ||
} else { | ||
console.error(err); | ||
} | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bash | ||
|
||
FULL_PATH="$(readlink -f ${BASH_SOURCE[0]})" | ||
MAIN_DIR=$(dirname "$FULL_PATH")/.. | ||
NODE_MODULES=$MAIN_DIR/node_modules | ||
|
||
POSITIONAL_ARGS=() | ||
INSPECT="" | ||
|
||
ORIG_ARGS=$@ | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--inspect) | ||
INSPECT="$1" | ||
shift # past argument | ||
;; | ||
*) | ||
if [[ -z "$CMD" ]]; then | ||
CMD=$1 | ||
fi | ||
POSITIONAL_ARGS+=("$1") # save positional arg | ||
shift # past argument | ||
;; | ||
esac | ||
done | ||
|
||
if test "$INSPECT" = "--inspect"; then | ||
/usr/bin/env node --inspect --no-warnings --enable-source-maps --experimental-specifier-resolution=node --loader ts-node/esm $MAIN_DIR/src/cli/odt2md.ts $ORIG_ARGS | ||
else | ||
/usr/bin/env node --no-warnings --enable-source-maps --experimental-specifier-resolution=node --loader ts-node/esm $MAIN_DIR/src/cli/odt2md.ts $ORIG_ARGS | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters