Skip to main content

Parse Markdown form and generate useful templates and data.

Project description

Latest Version License Python Versions CI Coverage

mdform

An extension for python-markdown to generate parse forms in Markdown based document.

This document:

Please fill this form:

name* = ___
email = @

And also this important question:

Do you like this = () YES () NO

will generate the following jinja template:

Please fill this form:

{{ form.name }}
{{ form.email }}

And also this important question:

{{ form.do_you_like_this }}

and this definition dictionary:

{'name': {'type': 'StringField',
          'required': True,
          'length': None
          },
 'email': {'type': 'EmailField',
           'required': False
          },
 'do_you_like_this': {'type': 'OptionField',
                      'required': False,
                      'items': ('YES', 'NO'),
                      'default': None
                      }
}

that you can consume to generate a form.

Installation

pip install mdform

Usage

>>> from mdform import FormExtension, Markdown # Markdown is just re-exported from python-markdown
>>> md = Markdown(extensions = [FormExtension()])
>>> html = md.convert(text)           # this is the jinja template
>>> form_dict = md.mdform_definition  # this is the definition dict

The html output will be a jinja2 template and a dictionary describing the form (mdform_definition)

Syntax

The syntax is strongly based on this wmd fork.

All fields are parsed into a dictionary (mdform_definition). The keys are the form labels and the value is another dictionary with the following elements:

  • type: str (e.g. StringField, TextAreaField, etc)

Text fields (StringField)

name = ___

or:

name = ___[50]

Exactly 3 underscores will be matched. Any more will be handled as standard underline directives.

Customizing:

name = ___[length]

Specific dict values:

  • length : int or None (default)

Integer fields (IntegerField)

value = ###

or:

value = ###[0:2:1]

Exactly 3 numeral will be matched. Any more will be handled as standard directives.

Customizing:

value = ###[range]

The range is parsed like a numpy range.

Specific dict values:

  • min : int or None (default)

  • max : int or None (default)

  • step : int or None (default)

Float fields (FloatField)

value = #.#

or:

value = #.#[0:2:0.5]

Exactly 3 numeral will be matched. Any more will be handled as standard directives.

Customizing:

value = #.#[range]

The range is parsed like a numpy range.

Specific dict values:

  • min : float or None (default)

  • max : float or None (default)

  • step : float or None (default)

Text area (TextAreaField)

name = AAA

or:

name = AAA[50]

Exactly 3 slashes will be matched.

Customizing:

name = ___[length]

Specific dict values:

  • length : int or None (default)

Radio buttons (RadioField)

sex = (x) male () female

Optionally, an x can be used to indicate the default value.

Specific dict values:

  • values : tuple of str

  • default : str

Check boxes (CheckboxField)

phones = [] Android [x] iPhone [x] Blackberry

Optionally, an x can be used to indicate the default values.

Specific dict values:

  • values : tuple of strings

  • default : tuple of str

File Field (FileField)

name = ...

or:

name = ...[png]
name = ...[png,jpg]
name = ...[png,jpg;Only image files]

Specific dict values:

  • allowed : tuple of str

  • description : str

Date Field (DateField)

name = d/m/y

Time Field (TimeField)

name = hh:mm

Required fields

To flag a field as required, just add an asterisk after the name.

zip code* = ___

Specific dict values:

  • required: bool

Sections

In certain cases is useful to create a named section.

[section:university]

name = ___

[section:school]

name = ___

will render as:

{{ form.university_name }} {{ form.school_name }}

and:

{'university_name': {'type': 'StringField',
                     'required': True,
                     'length': None
                     },
 'school_name': {'type': 'StringField',
                 'required': True,
                 'length': None
                 }
}

Sections are labeled from top to bottom, no remove a section name just do it this way.

[section:university]

name = ___

[section]

name = ___

will render as:

{{ form.university_name }}
{{ form.name }}

Collapsable parts

In certain cases is useful to create a part of the form which collapses based on the value of a dropdown box. Just use the modifier [c] for the dropdown item that will collapse the part of the html and enclose the collapsable part as shown:

extra = {Yes, (No[c])}

[collapse:extra]

name = ___

[endcollapse]

The extra in the collapse tag indicates which dropdown box is used as control.

See AUTHORS for a list of the maintainers.

To review an ordered list of notable changes for each version of a project, see CHANGES

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdform-0.5.tar.gz (15.2 kB view details)

Uploaded Source

File details

Details for the file mdform-0.5.tar.gz.

File metadata

  • Download URL: mdform-0.5.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mdform-0.5.tar.gz
Algorithm Hash digest
SHA256 53abe9f8fb36b0ae4767747a04c116f4c01890bdc43c39c7e5085d7d6a4f1763
MD5 d01f95b7979a9cc2dd0536968977c825
BLAKE2b-256 e1597e6a46cf18461bcefba592a554a9ebb42a37a3b17fd6337c56375c7040f7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page