<commit_before>/*
 * Copyright 2013-2016 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.rewrite

import laika.api.config.{Config, ConfigBuilder, ConfigParser}
import laika.api.builder.OperationConfig
import laika.ast._
import laika.ast.helper.DocumentViewBuilder.{Documents => Docs, _}
import laika.ast.helper.ModelBuilder
import org.scalatest.{FlatSpec, Matchers}

class SectionNumberSpec extends FlatSpec
                        with Matchers
                        with ModelBuilder {

  
  trait TreeModel {

    import Path.Root
    
    def header (level: Int, title: Int, style: String = "section") =
      Header(level,List(Text(s"Title $title")),Id(s"title$title") + Styles(style))
      
    def tree (content: RootElement): DocumentTree = {
      def docs (path: Path, nums: Int*) = nums map (n => Document(path / ("doc"+n), content))
      DocumentTree(Root, docs(Root, 1,2) ++ List(
        DocumentTree(Root / "sub1", docs(Root / "sub1",3,4)),
        DocumentTree(Root / "sub2", docs(Root / "sub2",5,6))
      ), config = parseConfig(config))
    }
    
    def numberedHeader (level: Int, title: Int, num: List[Int], style: String = "section"): Header = {
      val numbered = isIncluded(num.length) && (style == "section" && numberSections) || (style == "title" && numberDocs)
      val number = if (numbered) List(SectionNumber(num)) else Nil
      Header(level, number ++ List(Text(s"Title $title")),Id(s"title$title") + Styles(style))
    }
      
    def numberedSection (level: Int, title: Int, num: List[Int], children: Section*): Section =
      Section(numberedHeader(level, title, num), children)
      
    def numberedSectionInfo (level: Int, title: Int, num: List[Int], children: SectionInfo*): SectionInfo =
      SectionInfo(s"title$title", TitleInfo(numberedHeader(level, title, num).content), children)


    def treeView (content: List[Int] => List[DocumentContent]): TreeView = {
      def numbers (titleNums: List[Int]) = if (numberDocs) titleNums else Nil
      def docs (path: Path, nums: (Int, List[Int])*) = nums map { 
        case (fileNum, titleNums) => new DocumentView(path / ("doc"+fileNum), content(numbers(titleNums)))
      }
      TreeView(Root, Docs(DocumentType.Markup, docs(Root, (1,List(1)),(2,List(2)))) :: Subtrees(List(
        TreeView(Root / "sub1", Docs(DocumentType.Markup, docs(Root / "sub1",(3,List(3,1)),(4, List(3,2)))) :: Nil),
        TreeView(Root / "sub2", Docs(DocumentType.Markup, docs(Root / "sub2",(5,List(4,1)),(6, List(4,2)))) :: Nil)
      )) :: Nil)<commit_msg>Config API: adjust SectionNumberSpec for config changes - #98<commit_after>/*
 * Copyright 2013-2016 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.rewrite

import laika.api.config.{Config, ConfigBuilder, ConfigParser}
import laika.api.builder.OperationConfig
import laika.ast._
import laika.ast.helper.DocumentViewBuilder.{Documents => Docs, _}
import laika.ast.helper.ModelBuilder
import org.scalatest.{FlatSpec, Matchers}

class SectionNumberSpec extends FlatSpec
                        with Matchers
                        with ModelBuilder {

  
  trait TreeModel {

    import Path.Root
    
    def header (level: Int, title: Int, style: String = "section") =
      Header(level,List(Text(s"Title $title")),Id(s"title$title") + Styles(style))
      
    def tree (content: RootElement): DocumentTree = {
      val autonumberConfig = parseConfig(config)
      def docs (path: Path, nums: Int*) = nums map (n => Document(path / ("doc"+n), content, config = autonumberConfig))
      DocumentTree(Root, docs(Root, 1,2) ++ List(
        DocumentTree(Root / "sub1", docs(Root / "sub1",3,4), config = autonumberConfig),
        DocumentTree(Root / "sub2", docs(Root / "sub2",5,6), config = autonumberConfig)
      ), config = autonumberConfig)
    }
    
    def numberedHeader (level: Int, title: Int, num: List[Int], style: String = "section"): Header = {
      val numbered = isIncluded(num.length) && (style == "section" && numberSections) || (style == "title" && numberDocs)
      val number = if (numbered) List(SectionNumber(num)) else Nil
      Header(level, number ++ List(Text(s"Title $title")),Id(s"title$title") + Styles(style))
    }
      
    def numberedSection (level: Int, title: Int, num: List[Int], children: Section*): Section =
      Section(numberedHeader(level, title, num), children)
      
    def numberedSectionInfo (level: Int, title: Int, num: List[Int], children: SectionInfo*): SectionInfo =
      SectionInfo(s"title$title", TitleInfo(numberedHeader(level, title, num).content), children)


    def treeView (content: List[Int] => List[DocumentContent]): TreeView = {
      def numbers (titleNums: List[Int]) = if (numberDocs) titleNums else Nil
      def docs (path: Path, nums: (Int, List[Int])*) = nums map { 
        case (fileNum, titleNums) => DocumentView(path / ("doc"+fileNum), content(numbers(titleNums)))
      }
      TreeView(Root, Docs(DocumentType.Markup, docs(Root, (1,List(1)),(2,List(2)))) :: Subtrees(List(
        TreeView(Root / "sub1", Docs(DocumentType.Markup, docs(Root / "sub1",(3,List(3,1)),(4, List(3,2)))) :: Nil),
        TreeView(Root / "sub2", Docs(DocumentType.Markup, docs(Root / "sub2",(5,List(4,1)),(6, List(4,2)))) :: Nil)
      )) :: Nil)
{'fraction_non_alphanumeric': 0.13992156862745098, 'fraction_numerical': 0.013490196078431372, 'mean_word_length': 3.7159763313609466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2366230', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1924, 'n_words': 562}