Helper function to build a box containing a list of elements,
        with a title above and/or below, or left and/or right of the
        box. (e.g. a class name on top, or brackets on both sides.)

        The elements given must already have been transformed into
        Tag instances.

        Arguments:
            titles: A pair of strings to display on top and bottom
                (if tdir=='v') or left and right (if tdir=='h').
                If either or both titles are None, they will be
                omitted.
            contents: A list of Tags.
            tdir: tdir=='h' (default) means the titles will be on
                the left and right. tdir=='v' means they will be
                on top and bottom.
            cdir: cdir=='h' (default) means the contents will be
                stacked horizontally. cdir=='v' means they will
                be stacked vertically.

def titled_box(self, titles, contents, tdir='h', cdir='h'):
        """
        Helper function to build a box containing a list of elements,
        with a title above and/or below, or left and/or right of the
        box. (e.g. a class name on top, or brackets on both sides.)

        The elements given must already have been transformed into
        Tag instances.

        Arguments:
            titles: A pair of strings to display on top and bottom
                (if tdir=='v') or left and right (if tdir=='h').
                If either or both titles are None, they will be
                omitted.
            contents: A list of Tags.
            tdir: tdir=='h' (default) means the titles will be on
                the left and right. tdir=='v' means they will be
                on top and bottom.
            cdir: cdir=='h' (default) means the contents will be
                stacked horizontally. cdir=='v' means they will
                be stacked vertically.
        """
        H = self.H

        def wrapt(x):
            return H.div['hrepr-title'](x)

        rval = H.div[f'hrepr-titled-{tdir}']
        contents = H.div[f'hrepr-contents-{cdir}'].fill(contents)

        if isinstance(titles, tuple) and len(titles) == 2:
            open, close = titles
        else:
            open, close = titles, None

        if open:
            rval = rval(wrapt(open))
        rval = rval(contents)
        if close:
            rval = rval(wrapt(close))

        return rval