GithubHelp home page GithubHelp logo

html2markdown's Introduction

html2markdown for PHP

An HTML-to-markdown conversion tool for PHP by @nickcernis

Version 1.0.1
Requires PHP 5
Licensed under The MIT license: http://www.opensource.org/licenses/mit-license.php

###Instructions

Include html2markdown.php:

require_once( dirname( __FILE__) . '/html2markdown.php' );

Call html2markdown() on your valid, slash-stripped HTML code:

$markdown = html2markdown($html);

The $markdown string now contains the markdowned version of your HTML.

The included demo.php file contains an HTML->markdown conversion form for testing.

###Limitations

  • Chokes on malformed HTML. i.e. unclosed tags. Am considering a cleanup function to check for simple errors with an off switch for projects that perform their own reformatting, like WordPress. At present, you must nest <code> blocks inside <p> or <pre>, for example.
  • Markdown Extra, MultiMarkdown and other variants aren't supported; just Markdown.

###Bugs

  • Nested lists and lists with multiple paragraphs aren't converted correctly.
  • Lists inside blockquotes aren't converted correctly.

###Style notes

  • SETEX (underlined) headers are the default for H1 and H2. Change the HTML2MD_HEADER_STYLE constant to "ATX" if you want # H1 and ## H2 style tags instead.
  • Reference-style labels (with src and href links in the footnotes) are not used. Links and images are all referenced inline.
  • Blockquotes aren't line wrapped (makes them easier to edit).

###Architecture notes

html2markdown is about 450 lines. It's a single file with no dependencies. It uses native DOM manipulation libraries (DOMDocument), not regex voodoo, to convert code. Bug fixes and improvements are welcome.

###Contributors

My thanks to unlight and iamthes for their contributions.

###How it works html2markdown creates a DOM tree from the supplied HTML, walks through the tree, and converts each node to a text node, starting from the most deeply nested node and working inwards towards the root node.

###TO-DO

  • Support for nested lists and lists inside blockquotes.

###Trying to convert Markdown to HTML?

Use PHP Markdown from Michel Fortin.

html2markdown's People

Contributors

nickcernis avatar unlight avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.