0 people like it.

Strip HTML tags from a string

Shows a recursive means of filtering out simple HTML tags from a string. This is ultimately a simple FSM with state transitions implemented by recursive invocations and a bool flag (in this simple case).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.

open System

[<EntryPoint>]
let main argv = 
    
    let strip_tags input = 
        let rec get_next_character (flag, text) = 
            match flag with
            | _     when (Seq.isEmpty text)    -> None 
            | _     when (Seq.head text = '<') -> get_next_character (false,  Seq.tail text) 
            | _     when (Seq.head text = '>') -> get_next_character (true,   Seq.tail text) 
            | false                            -> get_next_character (false,  Seq.tail text) 
            | true                             -> Some (Seq.head text, (true, Seq.tail text)) 
        
        Seq.unfold get_next_character (true, input) |> Seq.toArray |> String 

    let result = strip_tags "This text is HTML with an embedded<img> image tag."

    0 // return an integer exit code
namespace System
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

Full name: Microsoft.FSharp.Core.EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : argv:string [] -> int

Full name: Script.main
val argv : string []
val strip_tags : (seq<char> -> String)
val input : seq<char>
val get_next_character : (bool * seq<char> -> (char * (bool * seq<char>)) option)
val flag : bool
val text : seq<char>
module Seq

from Microsoft.FSharp.Collections
val isEmpty : source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.isEmpty
union case Option.None: Option<'T>
val head : source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.head
val tail : source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.tail
union case Option.Some: Value: 'T -> Option<'T>
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val result : String
Raw view Test code New version

More information

Link:http://fssnip.net/7Qf
Posted:7 years ago
Author:Hugh Gleaves
Tags: html