src/Pure/Tools/doc.scala
author wenzelm
Sat Jul 27 21:01:35 2013 +0200 (2013-07-27)
changeset 52740 bceec99254b0
parent 52542 19d674acb764
child 53777 06a6216f733e
permissions -rw-r--r--
documentation is always in PDF;
wenzelm@52444
     1
/*  Title:      Pure/Tools/doc.scala
wenzelm@52427
     2
    Author:     Makarius
wenzelm@52427
     3
wenzelm@52444
     4
View Isabelle documentation.
wenzelm@52427
     5
*/
wenzelm@52427
     6
wenzelm@52427
     7
package isabelle
wenzelm@52427
     8
wenzelm@52427
     9
wenzelm@52427
    10
import scala.util.matching.Regex
wenzelm@52427
    11
wenzelm@52427
    12
wenzelm@52427
    13
object Doc
wenzelm@52427
    14
{
wenzelm@52427
    15
  /* dirs */
wenzelm@52427
    16
wenzelm@52427
    17
  def dirs(): List[Path] =
wenzelm@52427
    18
    Path.split(Isabelle_System.getenv("ISABELLE_DOCS")).map(dir =>
wenzelm@52427
    19
      if (dir.is_dir) dir
wenzelm@52427
    20
      else error("Bad documentation directory: " + dir))
wenzelm@52740
    21
wenzelm@52427
    22
wenzelm@52427
    23
  /* contents */
wenzelm@52427
    24
wenzelm@52444
    25
  private def contents_lines(): List[String] =
wenzelm@52444
    26
    for {
wenzelm@52444
    27
      dir <- dirs()
wenzelm@52444
    28
      catalog = dir + Path.basic("Contents")
wenzelm@52444
    29
      if catalog.is_file
wenzelm@52444
    30
      line <- split_lines(Library.trim_line(File.read(catalog)))
wenzelm@52444
    31
    } yield line
wenzelm@52444
    32
wenzelm@52427
    33
  sealed abstract class Entry
wenzelm@52427
    34
  case class Section(text: String) extends Entry
wenzelm@52427
    35
  case class Doc(name: String, title: String) extends Entry
wenzelm@52542
    36
  case class Text_File(name: String, path: Path) extends Entry
wenzelm@52427
    37
wenzelm@52444
    38
  private val Section_Entry = new Regex("""^(\S.*)\s*$""")
wenzelm@52444
    39
  private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
wenzelm@52427
    40
wenzelm@52542
    41
  private def release_notes(): List[Entry] =
wenzelm@52542
    42
  {
wenzelm@52542
    43
    def text_file(name: String): Option[Text_File] =
wenzelm@52542
    44
    {
wenzelm@52542
    45
      val path = Path.variable("ISABELLE_HOME") + Path.explode(name)
wenzelm@52542
    46
      if (path.is_file) Some(Text_File(name, path))
wenzelm@52542
    47
      else None
wenzelm@52542
    48
    }
wenzelm@52542
    49
    val names =
wenzelm@52542
    50
      List("ANNOUNCE", "README", "NEWS", "COPYRIGHT", "CONTRIBUTORS",
wenzelm@52542
    51
        "contrib/README", "README_REPOSITORY")
wenzelm@52542
    52
    Section("Release notes") ::
wenzelm@52542
    53
      (for (name <- names; entry <- text_file(name)) yield entry)
wenzelm@52542
    54
  }
wenzelm@52541
    55
wenzelm@52444
    56
  def contents(): List[Entry] =
wenzelm@52541
    57
    (for {
wenzelm@52444
    58
      line <- contents_lines()
wenzelm@52427
    59
      entry <-
wenzelm@52427
    60
        line match {
wenzelm@52427
    61
          case Section_Entry(text) => Some(Section(text))
wenzelm@52427
    62
          case Doc_Entry(name, title) => Some(Doc(name, title))
wenzelm@52427
    63
          case _ => None
wenzelm@52427
    64
        }
wenzelm@52542
    65
    } yield entry) ::: release_notes()
wenzelm@52427
    66
wenzelm@52427
    67
wenzelm@52427
    68
  /* view */
wenzelm@52427
    69
wenzelm@52427
    70
  def view(name: String)
wenzelm@52427
    71
  {
wenzelm@52740
    72
    val doc = name + ".pdf"
wenzelm@52448
    73
    dirs().find(dir => (dir + Path.basic(doc)).is_file) match {
wenzelm@52448
    74
      case Some(dir) =>
wenzelm@52429
    75
        Isabelle_System.bash_env(dir.file, null,
wenzelm@52429
    76
          "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
wenzelm@52448
    77
      case None => error("Missing Isabelle documentation file: " + quote(doc))
wenzelm@52427
    78
    }
wenzelm@52427
    79
  }
wenzelm@52444
    80
wenzelm@52444
    81
wenzelm@52444
    82
  /* command line entry point */
wenzelm@52444
    83
wenzelm@52444
    84
  def main(args: Array[String])
wenzelm@52444
    85
  {
wenzelm@52444
    86
    Command_Line.tool {
wenzelm@52444
    87
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
wenzelm@52444
    88
      else args.foreach(view)
wenzelm@52444
    89
      0
wenzelm@52444
    90
    }
wenzelm@52444
    91
  }
wenzelm@52427
    92
}
wenzelm@52427
    93