<commit_before>/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package laika.tree

import laika.tree.Elements._

/** Generic utilities that help with document tree modifications.
 * 
 *  @author <NAME>
 */
object TreeUtil {

  
  /** Returns a new instance of the customizable element
   *  without its id.
   */
  def removeId [C <: Customizable] (c: C) = modifyOptions(c, opt => Options(None,opt.styles))

  /** Returns a new instance of the customizable element
   *  with its id set to the specified value, overriding any existing value.
   */
  def setId [C <: Customizable] (c: C, id: String) = modifyOptions(c, opt => Options(Some(id), opt.styles))

  /** Returns a new instance of the customizable element
   *  with its options modified according to the specified function.
   */
  def modifyOptions [C <: Customizable] (c: C, f: Options => Options) = {
    val newElements = (c.productIterator map { 
      case opt:Options => f(opt)   
      case other => other  
    }).toArray
      
    c.getClass.getConstructors()(0)
      .newInstance(newElements.asInstanceOf[Array[AnyRef]]:_*).asInstanceOf[C]
  }
  
  
}<commit_msg>add TreeUtil.extractText to remove all formatting and links from a sequence of spans<commit_after>/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package laika.tree

import laika.tree.Elements._

/** Generic utilities that help with document tree modifications.
 * 
 *  @author <NAME>
 */
object TreeUtil {

  
  /** Returns a new instance of the customizable element
   *  without its id.
   */
  def removeId [C <: Customizable] (c: C) = modifyOptions(c, opt => Options(None,opt.styles))

  /** Returns a new instance of the customizable element
   *  with its id set to the specified value, overriding any existing value.
   */
  def setId [C <: Customizable] (c: C, id: String) = modifyOptions(c, opt => Options(Some(id), opt.styles))

  /** Returns a new instance of the customizable element
   *  with its options modified according to the specified function.
   */
  def modifyOptions [C <: Customizable] (c: C, f: Options => Options) = {
    val newElements = (c.productIterator map { 
      case opt:Options => f(opt)   
      case other => other  
    }).toArray
      
    c.getClass.getConstructors()(0)
      .newInstance(newElements.asInstanceOf[Array[AnyRef]]:_*).asInstanceOf[C]
  }
  
  /** Extracts the text from the specified sequence of spans, removing
   *  any formatting or links.
   */
  def extractText (spans: Seq[Span]): String = ("" /: spans) { (acc, span) => span match {
    case Text(content, _)     => acc + content
    case sc: SpanContainer[_] => acc + extractText(sc.content)
    case _ => acc
  }}
  
  
}
{'fraction_non_alphanumeric': 0.10120608285264814, 'fraction_numerical': 0.0047194546407970635, 'mean_word_length': 3.2913385826771653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6947400', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1061, 'n_words': 433}