REBOL [ Title: "HTML to plain text converter" File: %html-to-text.r Type: 'Module Purpose: { Defines a function that can convert a HTML text string into a plain text string. } Author: "Gabriele Santilli" License: { ================================= A message from Qtask about this source code: We have selected the MIT license (as of 2010-Jan-1) because it is the closest “standard” license to our intent. If we had our way, we would declare this source as public domain, with absolutely no strings attached, not even the string that says you have to have strings. We want to help people, so please feel free to contact us at API@Qtask.com if you have questions. (you only need to include the standard license text below in your homage to this source code) ================================= Copyright 2009 Qtask, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. } Version: 1.0.0 Imports: [ %dialects/fsm.r "Used for the state machine" %parsers/ml-parser.r "Used to parse the HTML text" %mezz/normalize-html.r "Used to normalize the HTML before converting it" ] Globals: [ html-to-text {Made global so that it's easy to use in non-modules} ] ] stage3: func [command command-data] [ process-event stage3-fsm command command-data ] init-stage3: does [ out: make string! 1024 uri-base: none clear links clear indent reset-fsm/only stage3-fsm ] end-stage3: does [ reset-fsm stage3-fsm emit newline forall links [ emit [#"[" index? links "] "] emit-uri links/1 emit newline ] out ] links: [] para-buffer: "" stage3-fsm: make-fsm [ initial-state: [ in-html ] in-html: [ in-head (unless empty? out [emit newline]) in-body return ] in-head: [ return in-title <base/> (uri-base: any [select data 'href uri-base]) <style> in-style ] in-style: [ </style> return ] in-title: [ return ] in-body: [ return