src/Pure/PIDE/document_editor.scala
changeset 77202 064566bc1f35
parent 77197 a541da01ba67
equal deleted inserted replaced
77201:2cf7a61e4a73 77202:064566bc1f35
    15 
    15 
    16   def document_name: String = "document"
    16   def document_name: String = "document"
    17   def document_output_dir(): Path = Path.explode("$ISABELLE_HOME_USER/document_output")
    17   def document_output_dir(): Path = Path.explode("$ISABELLE_HOME_USER/document_output")
    18   def document_output(name: String): Path = document_output_dir() + Path.basic(name)
    18   def document_output(name: String): Path = document_output_dir() + Path.basic(name)
    19 
    19 
    20   object Meta_Data {
    20   object Meta_Info {
    21     def read(name: String = document_name): Option[Meta_Data] = {
    21     def read(name: String = document_name): Option[Meta_Info] = {
    22       val json_path = document_output(name).json
    22       val json_path = document_output(name).json
    23       if (json_path.is_file) {
    23       if (json_path.is_file) {
    24         val json = JSON.parse(File.read(json_path))
    24         val json = JSON.parse(File.read(json_path))
    25         for {
    25         for {
    26           selection <- JSON.list(json, "selection", JSON.Value.String.unapply)
    26           selection <- JSON.list(json, "selection", JSON.Value.String.unapply)
    27           sources <- JSON.string(json, "sources")
    27           sources <- JSON.string(json, "sources")
    28           log <- JSON.string(json, "log")
    28           log <- JSON.string(json, "log")
    29           pdf <- JSON.string(json, "pdf")
    29           pdf <- JSON.string(json, "pdf")
    30         } yield {
    30         } yield {
    31           Meta_Data(name,
    31           Meta_Info(name,
    32             SortedSet.from(selection),
    32             SortedSet.from(selection),
    33             SHA1.fake_digest(sources),
    33             SHA1.fake_digest(sources),
    34             SHA1.fake_digest(log),
    34             SHA1.fake_digest(log),
    35             SHA1.fake_digest(pdf))
    35             SHA1.fake_digest(pdf))
    36         }
    36         }
    51           "pdf" -> SHA1.digest(doc.pdf).toString)
    51           "pdf" -> SHA1.digest(doc.pdf).toString)
    52       File.write(document_output(name).json, JSON.Format.pretty_print(json))
    52       File.write(document_output(name).json, JSON.Format.pretty_print(json))
    53     }
    53     }
    54   }
    54   }
    55 
    55 
    56   sealed case class Meta_Data(
    56   sealed case class Meta_Info(
    57     name: String,
    57     name: String,
    58     selection: SortedSet[String],
    58     selection: SortedSet[String],
    59     sources: SHA1.Digest,
    59     sources: SHA1.Digest,
    60     log: SHA1.Digest,
    60     log: SHA1.Digest,
    61     pdf: SHA1.Digest
    61     pdf: SHA1.Digest
    75     name: String = document_name
    75     name: String = document_name
    76   ): Unit = {
    76   ): Unit = {
    77     val output = document_output(name)
    77     val output = document_output(name)
    78     File.write(output.log, doc.log)
    78     File.write(output.log, doc.log)
    79     Bytes.write(output.pdf, doc.pdf)
    79     Bytes.write(output.pdf, doc.pdf)
    80     Meta_Data.write(selection, doc, name = name)
    80     Meta_Info.write(selection, doc, name = name)
    81   }
    81   }
    82 
    82 
    83   def view_document(name: String = document_name): Unit = {
    83   def view_document(name: String = document_name): Unit = {
    84     val path = document_output(name).pdf
    84     val path = document_output(name).pdf
    85     if (path.is_file) Isabelle_System.pdf_viewer(path)
    85     if (path.is_file) Isabelle_System.pdf_viewer(path)
   169     Isabelle_System.with_tmp_dir("document") { tmp_dir =>
   169     Isabelle_System.with_tmp_dir("document") { tmp_dir =>
   170       val engine = context.get_engine()
   170       val engine = context.get_engine()
   171       val variant = document_session.get_variant
   171       val variant = document_session.get_variant
   172       val directory = engine.prepare_directory(context, tmp_dir, variant, verbose = true)
   172       val directory = engine.prepare_directory(context, tmp_dir, variant, verbose = true)
   173       val ok =
   173       val ok =
   174         Meta_Data.read() match {
   174         Meta_Info.read() match {
   175           case Some(meta_data) =>
   175           case Some(meta_info) =>
   176             meta_data.check_files() && meta_data.sources == directory.sources
   176             meta_info.check_files() && meta_info.sources == directory.sources
   177           case None => false
   177           case None => false
   178         }
   178         }
   179       if (!ok) {
   179       if (!ok) {
   180         write_document(document_session.selection,
   180         write_document(document_session.selection,
   181           engine.build_document(context, directory, verbose = true))
   181           engine.build_document(context, directory, verbose = true))