posit-dev / py-htmltools

Tools for HTML generation and output
MIT License
19 stars 2 forks source link

Add HTMLTextDocument class #61

Closed wch closed 11 months ago

wch commented 11 months ago

The HTMLTextDocument class has a similar purpose to HTMLDocument, except that its contents are loaded from an HTML file.

It is meant to be used with https://github.com/posit-dev/py-shiny/pull/746

This PR also makes it possible change how HTML dependencies are rendered, by setting the following:

htmltools.html_dependency_render_mode = "json"

Normally HTML dependencies are not visible when a Tag/TagList calls .render(). However, if that value is set to "json", then HTML dependencies will be rendered into the document as something like this (note that the JSON has been prettified here, but normally is not):

<script type="application/json" data-html-dependency="">
{
  "name":"strftime",
  "version":"0.9.2",
  "source":{
    "package":"shiny",
    "subdir":"www/shared/strftime/"
  },
  "script":[
    {
      "src":"strftime-min.js"
    }
  ],
  "stylesheet":[    
  ],
  "meta":[    
  ],
  "all_files":false,
  "head":null
}
</script>

Then when the HTML is read in, the HTMLDependency object can be re-created like this:

args = json.loads(x)
dep = HTMLDependency(**args)