Skip to content

Commit

Permalink
Merge pull request #3 from nursingclio/stage/1.x
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
admturner authored Nov 22, 2020
2 parents e1a0886 + d6e88d0 commit 45f3d99
Show file tree
Hide file tree
Showing 4 changed files with 1,035 additions and 32 deletions.
108 changes: 108 additions & 0 deletions .npmpackagejsonlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"rules": {
"bin-type": "error",
"bundledDependencies-type": "error",
"config-type": "error",
"cpu-type": "error",
"dependencies-type": "error",
"description-type": "error",
"devDependencies-type": "error",
"directories-type": "error",
"engines-type": "error",
"files-type": "error",
"homepage-type": "error",
"keywords-type": "error",
"license-type": "error",
"main-type": "error",
"man-type": "error",
"name-format": "error",
"name-type": "error",
"no-restricted-dependencies": "off",
"no-restricted-devDependencies": "off",
"no-restricted-pre-release-dependencies": "off",
"no-restricted-pre-release-devDependencies": "off",
"optionalDependencies-type": "error",
"os-type": "error",
"peerDependencies-type": "error",
"prefer-absolute-version-dependencies": "off",
"prefer-absolute-version-devDependencies": "off",
"prefer-alphabetical-bundledDependencies": "error",
"prefer-alphabetical-dependencies": "error",
"prefer-alphabetical-devDependencies": "error",
"prefer-alphabetical-optionalDependencies": "error",
"prefer-alphabetical-peerDependencies": "error",
"prefer-caret-version-dependencies": "off",
"prefer-caret-version-devDependencies": "off",
"prefer-no-engineStrict": "off",
"prefer-no-version-zero-dependencies": "off",
"prefer-no-version-zero-devDependencies": "off",
"prefer-property-order": [
"error",
[
"name",
"version",
"description",
"author",
"license",
"homepage",
"repository",
"bugs",
"keywords",
"engines",
"directories",
"files",
"type",
"main",
"module",
"react-native",
"types",
"bin",
"dependencies",
"devDependencies",
"peerDependencies",
"publishConfig",
"scripts"
]
],
"prefer-tilde-version-dependencies": "off",
"prefer-tilde-version-devDependencies": "off",
"preferGlobal-type": "error",
"private-type": "error",
"repository-type": "error",
"require-author": "error",
"require-bin": "off",
"require-bugs": "error",
"require-bundledDependencies": "off",
"require-config": "off",
"require-contributors": "off",
"require-cpu": "off",
"require-dependencies": "off",
"require-description": "error",
"require-devDependencies": "off",
"require-directories": "off",
"require-engines": "off",
"require-files": "off",
"require-homepage": "error",
"require-keywords": "error",
"require-license": "error",
"require-main": "off",
"require-man": "off",
"require-module": "off",
"require-name": "error",
"require-optionalDependencies": "off",
"require-os": "off",
"require-peerDependencies": "off",
"require-preferGlobal": "off",
"require-private": "off",
"require-publishConfig": "off",
"require-repository": "error",
"require-repository-directory": "off",
"require-scripts": "off",
"require-version": "error",
"scripts-type": "error",
"valid-values-author": "off",
"valid-values-private": "off",
"version-format": "error",
"version-type": "error"
}
}
120 changes: 95 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,110 @@
#!/usr/bin/env node

/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const mammoth = require( 'mammoth' );
// const { resolve } = require('path');

const input = 'input';
const output = 'output';
/**
* Constants
*/
const INPUT_DIR = 'input';
const OUTPUT_DIR = 'output';

/**
* Converts docx files to html.
*
* @param {string} filePath
*/
const convert = ( filePath ) => {
// @TODO determine if Word uses the same style name for block quotations.
const options = {
styleMap: [
"p[style-name='Quotations'] => gblockquote"
]
};

return new Promise( ( resolve ) => {
mammoth.convertToHtml( { path: filePath }, options )
.then( function( result ) {
const html = result.value;
resolve( html );
} )
.done();
} );
};

/**
* Clean and formats the raw HTML for Nursing Clio WP.
*
* @param {string} rawHtml
*/
const cleanHtml = ( rawHtml ) => {
return new Promise( ( resolve ) => {
const html = rawHtml.replace( /<\/ol>$/, '</ol>\n</section>' )
.replace( /<ol><li id="footnote/, '<section id="sources">\n<h4>Notes</h4><ol class="footnotes"><li id="footnote' )
.replace( /<li id="footnote-(\d+)">.*?<p>/g, '<li id="footnote-$1">' )
.replace( /<\/a><\/p><\/li>/g, '</a></li>' )
.replace( /<p>/g, '' )
.replace( /<\/p>/g, '\n\n' )
.replace( /<sup><sup>/g, '<sup>' )
.replace( /<\/sup><\/sup>/g, '</sup>' )
.replace( /id="footnote-ref/g, 'class="footnote-ref" id="footnote-ref' )
.replace( /">↑/g, '" class="return-link">Return to text.' )
.replace( /<gblockquote>/g, '[gblockquote]' )
.replace( /<\/gblockquote>/g, '[/gblockquote]\n\n')
.replace( /\t/g, ' ' )
.replace( / \n/g, '' )
.replace( /\n+/g, '\n\n' )
.replace( /<\/li>/g, '</li>\n' )
.replace( /<\/h(\d)>/g, '</h$1>\n\n' );
resolve( html );
} );
};

/**
* Writes the HTML content to the output directory.
*
* @param {string} content
* @param {string} file
*/
const writeFile = ( content, file ) => {
const fileName = path.basename( file, '.docx' );
const outputPath = path.format( {
dir: OUTPUT_DIR,
name: fileName,
ext: '.html',
} );

fs.writeFile( outputPath, content, err => {
if ( err ) {
return console.log( err );
}
} );
};

const init = async function() {
try {
const files = await fs.promises.readdir( input );
const files = await fs.promises.readdir( INPUT_DIR );

for( const file of files ) {
const inputPath = path.join( input, file );
const stat = await fs.promises.stat( inputPath );
const filePath = path.join( INPUT_DIR, file );
const stat = await fs.promises.stat( filePath );

// We only process docx files.
// Only process docx files.
if ( stat.isFile() && '.docx' === path.extname( file ) ) {
const fileName = path.basename( file, '.docx' );
const outputPath = path.format( {
dir: output,
name: fileName,
ext: '.html',
} );

mammoth.convertToHtml( { path: inputPath } )
.then( function( result ) {
const html = result.value;

fs.writeFile( outputPath, html, err => {
if ( err ) {
return console.log( err );
}
} );
} )
.done();
// Convert the docx to html using Mammoth.
const rawHtml = await convert( filePath );

// Run text replacements.
const html = await cleanHtml( rawHtml );

// Write content to output.
writeFile( html, file );

} else if ( '.gitkeep' !== file ) {
console.log( `Ignored "${file}": Only docx files can be converted. Other files and folders will be skipped.` );
}
Expand All @@ -43,6 +113,6 @@ const init = async function() {
catch( e ) {
console.error( 'Oops', e );
}
}
};

init();
Loading

0 comments on commit 45f3d99

Please sign in to comment.