shraddheya shrivastava
shraddheya shrivastava

Reputation: 187

Can I create a `gramex` `microservice` that automatically takes the subdirectories and renders the `markdown` in them?

I am trying to create a Gramex application. The intention is to:

  1. See the root folder readme.md page when I access localhost:9988 (local gramex generic port)
  2. See the readme.md in subdir1 when I access localhost:9988/subdir1/
  3. See the readme.md in subdir2 when I access localhost:9988/subdir2/

For that I have a gramex.yaml file, that reads

url:
  app:
    pattern: /$YAMLURL/
    handler: FileHandler
    kwargs:
      index: false
      path:
        "": $YAMLPATH/{dir}/readme.md
      transform:
        "*.md":
          encoding: utf-8
          function: markdown.markdown(content)
          headers:
            Content-Type: text/html; charset=UTF-8
      default:
        dir: ""
        file: readme
        ext: md

Here localhost:9988 renders the root folder readme.md.
However, browsing localhost:9988:subdir1 renders the directory index, implying that subdirs are not working.
Wondering if this is possible or not.

Upvotes: 1

Views: 18

Answers (1)

S Anand
S Anand

Reputation: 11948

You need to change 3 things:

  1. pattern: /$YAMLURL/ should be pattern: /$YAMLURL/.*. Otherwise, no other URL will match
  2. path: "": $YAMLPATH/{dir}/readme.md is not required
  3. default: is not a kwarg. Use default_filename: readme.md

Here's a working configuration:

url:
  app:
    pattern: /$YAMLURL/(.*)
    handler: FileHandler
    kwargs:
      path: $YAMLPATH
      default_filename: README.md
      transform:
        "*.md":
          encoding: utf-8
          function: markdown.markdown(content)
          headers:
            Content-Type: text/html; charset=UTF-8

The documentation is at https://gramener.com/gramex/guide/filehandler/

Upvotes: 0

Related Questions