Knio / dominate

Dominate is a Python library for creating and manipulating HTML documents using an elegant DOM API. It allows you to write HTML pages in pure Python very concisely, which eliminate the need to learn another template language, and to take advantage of the more powerful features of Python.
GNU Lesser General Public License v3.0
1.72k stars 108 forks source link

Allow to specify document language #144

Closed roskakori closed 2 years ago

roskakori commented 3 years ago

According to "Declaring language in HTML" the recommended way to specify the language of an HTML document is:

<html lang="en">...</html>

However, the document tag does not provide any means to set it:

class document(tags.html):
  tagname = 'html'
  def __init__(self, title='Dominate', doctype='<!DOCTYPE html>', request=None):

It would be nice to have an optional parameter lang=None that if set to e.g. lang='en' would set the respective attribute for <html>.

AFAICT this would be a rather simple change in document.render(), so if you want I can look into it and submit a pull request.

Knio commented 3 years ago

document inherits from the tag class, so all the regular attribute setting methods work:

d = dominate.document()
...
d['lang'] = 'en'
...
with d:
     dominate.tags.attr(lang='en')
...
d.set_attribute('lang','en')
...
print(d.render())
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dominate</title>
  </head>
  <body></body>
</html>

Perhaps a fix to make it more convenient would be to just pass **kwargs to the super in the document constructor (and maybe *args into the body tag)

roskakori commented 2 years ago

Thanks!