Domanda

esiste un modo semplice per trasformare l'HTML in markdown con JAVA?

Attualmente sto utilizzando Java MarkdownJ libreria per trasformare markdown in html.

import com.petebevin.markdown.MarkdownProcessor;
...
public static String getHTML(String markdown) {
    MarkdownProcessor markdown_processor = new MarkdownProcessor();
    return markdown_processor.markdown(markdown);
}

public static String getMarkdown(String html) {
/* TODO Ask stackoverflow */
}
È stato utile?

Soluzione

Usa questo XSLT.

Se hai bisogno di aiuto con XSLT e Java ecco uno snippet di codice:

public static void main(String[] args) throws Exception {

        File xsltFile = new File("mardownXSLT.xslt");

        Source xmlSource = new StreamSource(new StringReader(theHTML));
        Source xsltSource = new StreamSource(xsltFile);

        TransformerFactory transFact =
                TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);

        StringWriter result = new StringWriter();
        trans.transform(xmlSource, new StreamResult(result));
    }

Altri suggerimenti

Mi sono imbattuto in osservazione per aver convertito HTML in Markdown See: http://remark.overzealous.com/manual/index.htmlDipende da JSoup, una potente libreria Java per lavorare con l'HTML del mondo reale.

Sto lavorando sullo stesso problema e sperimentando un paio di tecniche diverse.

La risposta sopra potrebbe funzionare.Potresti usare il libreria jTidy per eseguire il lavoro di pulizia iniziale e convertire da HTML a XHTML.Usi il Foglio di stile XSLT collegato sopra.

Sfortunatamente non esiste una libreria che abbia una funzione unica per farlo in Java.Potresti provare a utilizzare lo script Python html2testo con Jython, ma non l'ho ancora provato!

se stai utilizzando l'editor WMD e desideri ottenere il codice di markdown sul lato server, utilizza semplicemente queste opzioni prima di caricare il file wmd.js sceneggiatura:

wmd_options = {
        // format sent to the server.  can also be "HTML"
        output: "Markdown",

        // line wrapping length for lists, blockquotes, etc.
        lineLength: 40,

        // toolbar buttons.  Undo and redo get appended automatically.
        buttons: "bold italic | link blockquote code image | ol ul heading hr",

        // option to automatically add WMD to the first textarea found.
        autostart: true
    };
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top