import { TextEditor , Uri , ViewColumn } from "vscode" ; import { DocumentContentManagerInterface } from "./documentContentManagerInterface" ; import { HtmlPreview } from "../util/htmlPreview" ; import { Docutils } from "../util/docutils" ; let rst2mdown = require ( "rst2mdown" ) ; let Markdown2HtmlLess = require ( "markdown2html-less" ) . Markdown2HtmlLess ; const markdown2htmlLess = new Markdown2HtmlLess ( ) ; export class ReStructuredTextDocumentContentManager implements DocumentContentManagerInterface { private _editor : TextEditor ; public constructor ( editor ) { this . _editor = editor ; return this ; } public editor ( ) { return this . _editor ; } public async createContentSnippet ( ) { let editor = this . _editor ; if ( ! editor ) { return HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ; } return this . generatePreviewSnippet ( editor ) ; } public sendPreviewCommand ( previewUri , displayColumn ) { return HtmlPreview . sendPreviewCommand ( previewUri , displayColumn ) ; } private getWindowErrorMessage ( ) { return `No Active editor - no properties to preview.` ; } private rstSrcSnippetWithNodeModules ( rstContent ) { const html = markdown2htmlLess . markdown2html ( rst2mdown ( rstContent ) ) ; html . head = html . head || '' ; html . body = html . body || '' ; return `${ html . head }
${ html . body }` ; } private rstSrcSnippetWithDocutils ( editor ) { if ( ! editor || ! editor . document ) { return Promise . resolve ( HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ) ; } let doc = editor . document ; return Docutils . rst2html ( doc . fileName ) ; } private rstSrcSnippet ( editor ) { if ( ! editor ) { return Promise . resolve ( HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ) ; } let thiz = this ; return this . rstSrcSnippetWithDocutils ( editor ) . catch ( function ( error ) { console . info ( "try rst2html of docutils failed, please check python and docutils environment: " + error ) ; console . info ( ", we use a simple preview instead ^-)" ) ; if ( ! editor . document ) { return Promise . resolve ( HtmlPreview . errorSnippet ( thiz . getWindowErrorMessage ( ) ) ) ; } return thiz . rstSrcSnippetWithNodeModules ( editor . document . getText ( ) ) ; } ) ; } private generatePreviewSnippet ( editor ) { if ( ! editor || ! editor . document ) { return Promise . resolve ( HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ) ; } let doc = editor . document ; return this . rstSrcSnippet ( editor ) . then ( function ( rstSrc ) { return HtmlPreview . fixNoneNetLinks ( rstSrc , doc . fileName ) ; } ) ; } }