0

I want to use a code block to execute other code blocks with a given language. For example, in my document I want to validate every HTML block using the W3 validator. Currently I do it as follows.

First a code block to validate.

#+name: mininal-html-5
#+begin_src html
<!DOCTYPE html>

<title>Minimal HTML 5</title>

<p>Is it valid at all?</p>
#+end_src

Next a shell script that validates, getting it's input as a variable. Here together with the result

#+name: validate
#+begin_src shell :results output code :wrap src json :var content=mininal-html-5[]
http --form --multipart --ignore-stdin POST https://validator.w3.org/nu/ out='json' content=${content} | jq
#+end_src

#+RESULTS: validate
#+begin_src json
{
  "messages": [
    {
      "type": "info",
      "lastLine": 3,
      "firstLine": 1,
      "lastColumn": 7,
      "firstColumn": 16,
      "subType": "warning",
      "message": "Consider adding a “lang” attribute to the “html” start tag to declare the language of this document.",
      "extract": "TYPE html>\n\n<title>Hello?",
      "hiliteStart": 10,
      "hiliteLength": 9
    }
  ],
  "source": {
    "type": "text/html",
    "encoding": "utf-8",
    "code": "<!DOCTYPE html>\n\n<title>Hello?</title>\n\n<p>Is it valid at all?</p>"
  }
}
#+end_src

The I can reuse it to validate another HTML block:

#+name: html-with-root
#+begin_src html
<!DOCTYPE html>
<html lang="en">
  <title>Rooted HTML 5</title>

  <p>Is it more valid?</p>
</html>
#+end_src

#+call: validate(content=html-with-root[])

#+RESULTS:
#+begin_src json
{
  "messages": [],
  "source": {
    "type": "text/html",
    "encoding": "utf-8",
    "code": "<!DOCTYPE html>\n<html lang=\"en\">\n  <title>Rooted HTML 5</title>\n\n  <p>Is it more valid?</p>\n</html>"
  }
}
#+end_src

It's quite good, but every time I need to give each block a unique name and then call the validator, referring to that name with square brackets. What I would really like, is to just evaluate the HTML code block and get the validation as a result. I think the way is to do something like:

(defun org-babel-execute:html (body params)
  "Validate given HTML using W3C Validator"
  (some-magic-function validate body))

The closest to "some-magic-function" I found so far is the org-sbe macro, but it's designed for table formulas, and in any case I couldn't get it to work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.