src/Pure/Thy/presentation.scala
changeset 72683 b5e6f0d137a7
parent 72682 e0443773ef1a
child 72695 45cd55248ffd
equal deleted inserted replaced
72682:e0443773ef1a 72683:b5e6f0d137a7
    11 
    11 
    12 
    12 
    13 object Presentation
    13 object Presentation
    14 {
    14 {
    15   /* document variants */
    15   /* document variants */
       
    16 
       
    17   type Document_PDF = (Document_Variant, Bytes)
    16 
    18 
    17   object Document_Variant
    19   object Document_Variant
    18   {
    20   {
    19     def parse(name: String, tags: String): Document_Variant =
    21     def parse(name: String, tags: String): Document_Variant =
    20       Document_Variant(name, Library.space_explode(',', tags))
    22       Document_Variant(name, Library.space_explode(',', tags))
    77         val sources = res.string(Data.sources)
    79         val sources = res.string(Data.sources)
    78         Document_Variant.parse(name, tags).set_sources(sources)
    80         Document_Variant.parse(name, tags).set_sources(sources)
    79       }).toList)
    81       }).toList)
    80   }
    82   }
    81 
    83 
    82   def read_document(db: SQL.Database, session_name: String, name: String)
    84   def read_document(db: SQL.Database, session_name: String, name: String): Option[Document_PDF] =
    83     : Option[(Document_Variant, Bytes)] =
       
    84   {
    85   {
    85     val select = Data.table.select(sql = Data.where_equal(session_name, name))
    86     val select = Data.table.select(sql = Data.where_equal(session_name, name))
    86     db.using_statement(select)(stmt =>
    87     db.using_statement(select)(stmt =>
    87     {
    88     {
    88       val res = stmt.execute_query()
    89       val res = stmt.execute_query()
   232   }
   233   }
   233 
   234 
   234   def session_html(
   235   def session_html(
   235     session: String,
   236     session: String,
   236     deps: Sessions.Deps,
   237     deps: Sessions.Deps,
   237     store: Sessions.Store,
   238     db_context: Sessions.Database_Context,
   238     presentation: Context) =
   239     presentation: Context)
   239   {
   240   {
   240     val info = deps.sessions_structure(session)
   241     val info = deps.sessions_structure(session)
   241     val options = info.options
   242     val options = info.options
   242     val base = deps(session)
   243     val base = deps(session)
   243 
   244 
   244     val session_dir = presentation.dir(store, info)
   245     val session_dir = presentation.dir(db_context.store, info)
   245     val session_fonts = Isabelle_System.make_directory(session_dir + Path.explode("fonts"))
   246     val session_fonts = Isabelle_System.make_directory(session_dir + Path.explode("fonts"))
   246     for (entry <- Isabelle_Fonts.fonts(hidden = true))
   247     for (entry <- Isabelle_Fonts.fonts(hidden = true))
   247       File.copy(entry.path, session_fonts)
   248       File.copy(entry.path, session_fonts)
   248 
   249 
   249     Bytes.write(session_dir + session_graph_path,
   250     Bytes.write(session_dir + session_graph_path,
   250       graphview.Graph_File.make_pdf(options, base.session_graph_display))
   251       graphview.Graph_File.make_pdf(options, base.session_graph_display))
   251 
   252 
   252     val documents =
   253     val documents =
   253       using(store.open_database(session))(db =>
   254       for {
   254         for {
   255         doc <- info.document_variants
   255           doc <- info.document_variants
   256         (_, pdf) <- db_context.read_document(session, doc.name)
   256           (_, pdf) <- Presentation.read_document(db, session, doc.name)
   257       } yield { Bytes.write(session_dir + doc.path.pdf, pdf); doc }
   257         } yield { Bytes.write(session_dir + doc.path.pdf, pdf); doc })
       
   258 
   258 
   259     val links =
   259     val links =
   260     {
   260     {
   261       val deps_link =
   261       val deps_link =
   262         HTML.link(session_graph_path, HTML.text("theory dependencies"))
   262         HTML.link(session_graph_path, HTML.text("theory dependencies"))
   436   def document_tex_name(name: Document.Node.Name): String = "document/" + tex_name(name)
   436   def document_tex_name(name: Document.Node.Name): String = "document/" + tex_name(name)
   437 
   437 
   438   def build_documents(
   438   def build_documents(
   439     session: String,
   439     session: String,
   440     deps: Sessions.Deps,
   440     deps: Sessions.Deps,
   441     store: Sessions.Store,
   441     db_context: Sessions.Database_Context,
   442     progress: Progress = new Progress,
   442     progress: Progress = new Progress,
   443     output_sources: Option[Path] = None,
   443     output_sources: Option[Path] = None,
   444     output_pdf: Option[Path] = None,
   444     output_pdf: Option[Path] = None,
   445     verbose: Boolean = false,
   445     verbose: Boolean = false,
   446     verbose_latex: Boolean = false): List[(Document_Variant, Bytes)] =
   446     verbose_latex: Boolean = false): List[Document_PDF] =
   447   {
   447   {
   448     /* session info */
   448     /* session info */
   449 
   449 
   450     val info = deps.sessions_structure(session)
   450     val info = deps.sessions_structure(session)
   451     val options = info.options
   451     val options = info.options
   454 
   454 
   455 
   455 
   456     /* prepare document directory */
   456     /* prepare document directory */
   457 
   457 
   458     lazy val tex_files =
   458     lazy val tex_files =
   459       using(store.open_database_context(deps.sessions_structure))(context =>
   459       for (name <- base.session_theories ::: base.document_theories)
   460         for (name <- base.session_theories ::: base.document_theories)
   460       yield {
   461         yield {
   461         val entry = db_context.get_export(session, name.theory, document_tex_name(name))
   462           val entry = context.export(session, name.theory, document_tex_name(name))
   462         Path.basic(tex_name(name)) -> entry.uncompressed(cache = db_context.xz_cache)
   463           Path.basic(tex_name(name)) -> entry.uncompressed(cache = store.xz_cache)
   463       }
   464         }
       
   465       )
       
   466 
   464 
   467     def prepare_dir1(dir: Path, doc: Document_Variant): (Path, String) =
   465     def prepare_dir1(dir: Path, doc: Document_Variant): (Path, String) =
   468     {
   466     {
   469       val doc_dir = dir + Path.basic(doc.name)
   467       val doc_dir = dir + Path.basic(doc.name)
   470       Isabelle_System.make_directory(doc_dir)
   468       Isabelle_System.make_directory(doc_dir)
   519 
   517 
   520 
   518 
   521           // old document from database
   519           // old document from database
   522 
   520 
   523           val old_document =
   521           val old_document =
   524             using(store.open_database(session))(db =>
   522             for {
   525               for {
   523               document@(doc, pdf) <- db_context.read_document(session, doc.name)
   526                 document@(doc, pdf) <- read_document(db, session, doc.name)
   524               if doc.sources == sources
   527                 if doc.sources == sources
   525             }
   528               }
   526             yield {
   529               yield {
   527               Bytes.write(doc_dir + doc.path.pdf, pdf)
   530                 Bytes.write(doc_dir + doc.path.pdf, pdf)
   528               document
   531                 document
   529             }
   532               })
       
   533 
   530 
   534           old_document getOrElse {
   531           old_document getOrElse {
   535             // bash scripts
   532             // bash scripts
   536 
   533 
   537             def root_bash(ext: String): String = Bash.string(root + "." + ext)
   534             def root_bash(ext: String): String = Bash.string(root + "." + ext)
   659 
   656 
   660         if (output_sources.isEmpty && output_pdf.isEmpty) {
   657         if (output_sources.isEmpty && output_pdf.isEmpty) {
   661           progress.echo_warning("No output directory")
   658           progress.echo_warning("No output directory")
   662         }
   659         }
   663 
   660 
   664         build_documents(session, deps, store, progress = progress,
   661         using(store.open_database_context(deps.sessions_structure))(db_context =>
   665           output_sources = output_sources, output_pdf = output_pdf,
   662           build_documents(session, deps, db_context, progress = progress,
   666           verbose = true, verbose_latex = verbose_latex)
   663             output_sources = output_sources, output_pdf = output_pdf,
       
   664             verbose = true, verbose_latex = verbose_latex))
   667       }
   665       }
   668     })
   666     })
   669 }
   667 }