protected function writeContent($subtitle, $subdoc, $subnode, $sort = null, $publishPages = false)
    {
        $record = $this->form->getRecord();

        if ($subtitle) {
            // Write the chapter page to a subpage.
            $page = DataObject::get_one(
                'Page',
                sprintf('"Title" = \'%s\' AND "ParentID" = %d', $subtitle, $record->ID)
            );
            if (!$page) {
                $page = Page::create();
                $page->ParentID = $record->ID;
                $page->Title = $subtitle;
            }

            unset($this->unusedChildren[$page->ID]);
            file_put_contents(ASSETS_PATH . '/index-' . $sort . '.html', $this->getBodyText($subdoc, $subnode));

            if ($sort) {
                $page->Sort = $sort;
            }
            $page->Content = $this->getBodyText($subdoc, $subnode);
            $page->write();
            if ($publishPages) {
                $page->publishRecursive();
            }
        } else {
            // Write to the master page.
            $record->Content = $this->getBodyText($subdoc, $subnode);
            $record->write();

            if ($publishPages) {
                $record->publishRecursive();
            }
        }
    }