Exemplo n.º 1
0
 def render_workflow_documentation(self, workflow: Workflow) -> str:
     """Generate a document fragment containing documentation for a single workflow."""
     if workflow is not None:
         annotation = workflow.get_annotation() if workflow.get_annotation(
         ) is not None else ""
         return OUTPUT_TEMPLATE_WF.format(
             e(workflow.display_name or ""),
             e(self.get_workflow_relative_path(workflow)), e(annotation),
             self.render_arguments(workflow.get_arguments()),
             self.get_workflow_id(workflow))
     return ""
Exemplo n.º 2
0
 def build_documentation(self):
     """Generate the documentation from the project."""
     self.__workflow_toc, self.__workflow_doc = tee(
         self.project.workflow_files)
     with open(self.target_path, 'w') as documentation_file:
         main_workfow: Workflow = self.get_main_workflow()
         documentation_file.write(
             OUTPUT_TEMPLATE.format(
                 e(self.project.name), e(self.project.version),
                 e(self.project.description),
                 self.render_workflow_documentation(main_workfow),
                 str.join('', [
                     self.render_workflow_documentation(wf)
                     for wf in self.__workflow_doc
                     if wf.file_path != main_workfow.file_path
                 ]), self.render_documentation_toc()))
     logging.info(self.render_documentation_toc())
Exemplo n.º 3
0
 def inner_render(self, node):
     text = e(node["text"])
     marks = node.get("marks")
     if marks:
         for mark in marks:
             tag = self.mark_tags.get(mark.get("type"))
             attrs = mark.get("attrs")
             if attrs:
                 if tag == "a":
                     url = attrs.get("href") or ""
                     if not is_trusted_link(url):
                         attrs["target"] = "_blank"
                         attrs["rel"] = "noopener nofollow"
                 attrs_s = " ".join(f'{k}="{e(v)}"'
                                    for k, v in attrs.items())
                 text = f"<{tag} {attrs_s}>{text}</{tag}>"
             else:
                 text = f"<{tag}>{text}</{tag}>"
     return text
Exemplo n.º 4
0
 def inner_render(self, node) -> str:
     attrs = node['attrs']
     level = attrs.get('level') or 1
     tag = e(f"h{level}")
     inner_html = super().inner_render(node)
     return f"<{tag}>{inner_html}</{tag}>"
Exemplo n.º 5
0
 def inner_render(self, node) -> str:
     return e(node["content"]["text"])
Exemplo n.º 6
0
    def get_body(self, root_elt, item):
        """This method generate the body according to NTB requirements and add images to associations"""
        body_list = [unescape(root_elt.find('body').text)]

        # images
        images = []
        ntb_media = []
        for image_elt in root_elt.xpath('images/image'):
            image_id = e(image_elt.get('id'))
            url = image_elt.findtext('url', '')
            e_url = e(url)
            caption = image_elt.findtext('caption') or e_url
            mime_type = mimetypes.guess_type(url, strict=False)[0]
            images.append('<a href="{url}">{caption}</a>'.format(
                url=e_url, caption=e(caption)))
            ntb_media.append({
                "id": image_id,
                "url": url,
                "mime_type": mime_type,
                "description_text": caption,
            })
        if images:
            body_list.extend(
                ['<p class="ntb-media">', "<br>".join(images), "</p>"])
            item.setdefault("extra", {}).setdefault("ntb_media",
                                                    []).extend(ntb_media)

        # contacts
        contacts = root_elt.xpath('contacts/contact')
        contacts_as_text = root_elt.xpath('contactsAsText/text()')
        if contacts or contacts_as_text:
            body_list.append('<h2>Kontakter</h2>')
            for contact_elt in contacts:
                body_list.append('<p><name>{name}</name><br>'
                                 '<title>{title}</title><br>'
                                 '<phone>{phone}</phone><br>'
                                 '<email>{email}</email>'
                                 '</p>'.format(
                                     name=e(contact_elt.findtext('name', '')),
                                     title=e(contact_elt.findtext('title',
                                                                  '')),
                                     phone=e(contact_elt.findtext('phone',
                                                                  '')),
                                     email=e(contact_elt.findtext('email',
                                                                  '')),
                                 ))
            for contact_txt in contacts_as_text:
                body_list.append(
                    '<p>{contact}</p>'.format(contact=e(contact_txt)))

        # documents
        documents = []
        for document_elt in root_elt.xpath('documents/document'):
            url = e(document_elt.findtext('url', ''))
            documents.append('<a href="{url}">{caption}</a>'.format(
                url=url, caption=e(document_elt.findtext('title') or url)))
        if documents:
            body_list.extend(
                ["<h2>Dokumenter</h2><p>", "<br>".join(documents), "</p>"])

        # longurl
        body_list.append('<p>Se saken i sin helhet:<br><a href="{longurl}">'
                         '{longurl}</a></p>'.format(
                             name=e(root_elt.findtext('publisher/name', '')),
                             longurl=e(root_elt.findtext('longurl', ''))))

        item['body_html'] = '\n'.join(body_list)
Exemplo n.º 7
0
def file(request):
    # study the request
    # D("request %s", request)
    # D("request.path %s", request.path)
    # D("request.headers %s", request.headers)
    # D("request.headers[X-Scheme] %s", request.headers.get("X-Scheme", None))
    # D("request %s", dir(request))
    # D("request GET %s", request.GET)

    # get file path to study
    path = Path(request.match_info["rpath"])

    # construct url root
    root = request.headers.get("X-Scheme", request.scheme) + "://" + \
        request.host + \
        request.headers.get("X-Root", "")

    # debug access
    D("root=%s path=%s", root, path)

    # make common data for all templates
    data = {
        "path": path,
        "title": path,
        "root": root,
        "hostname": socket.gethostname()
    }

    # sorted dir and regular file lists
    def files_dirs_regs(filelist):
        dirs = []
        regs = []
        files = []
        for i in filelist:
            files.append(i)
            if i.is_dir():
                dirs.append(i)
            else:
                regs.append(i)
        dirs.sort(key=lambda x: x.name)
        regs.sort(key=lambda x: x.name)
        files.sort(key=lambda x: x.name)
        return files, dirs, regs

    # get file from path
    tmpl = None
    try:
        file = File(path)
        if file.is_dir():
            tmpl = dir_tmpl
            files, dirs, regs = files_dirs_regs(file.listdir())
            data.update({
                "file": file,
                "dirs": dirs,
                "regs": regs,
                "files": files
            })
        else:  # is regular file
            tmpl = file_tmpl
            files, dirs, regs = files_dirs_regs(file.up().listdir())
            data.update({
                "file": file,
                "content": get_content(file).as_html(),
                "files": files,
                "dirs": dirs,
                "regs": regs
            })
    except FileNotFoundError as ex:
        tmpl = error_tmpl
        data["error"] = e(str(ex))
    except Exception as ex:
        tmpl = error_tmpl
        data["error"] = e(str(ex))

    # make response from rendered template
    return web.Response(text=tmpl.render(data), content_type="text/html")
Exemplo n.º 8
0
 def render_arguments(arguments: Iterable[WorkflowArgument]) -> str:
     """Generate a document fragment for the arguments of the workflow."""
     render_argument = lambda arg: OUTPUT_TEMPLATE_ARG.format(
         e(arg.name), e(arg.direction), e(arg.type),
         e(arg.annotation if arg.annotation is not None else ""))
     return str.join('', map(render_argument, arguments))