[spec] [whitespace]
Plain Text text at sdfeu.org
Wed Oct 13 22:12:03 BST 2021
On Mon, 11 Oct 2021 15:15:44 +0000, Plain Text wrote:
> https://gemini.circumlunar.space/docs/specification.gmi
My try on identifying line types using Python re named groups
what became a quite unreadable line, also missing ```, sorry.
line.py
import re, sys
for line in sys.stdin:
m = re.match(r'((?P<heads>(?P<h3>###)|(?P<h2>##)|(?P<h1>#))|(?P<list>\* )|(?P<link>=> (?P<url>[^\s]+))|(?P<quote>>))\s*(?P<content>.*)$', line)
if m:
print(m.groupdict())
example.gmi
# Heading of Level One
## Heading of Level Two
### Heading of Level Three
###Heading of Level Three Tight
####Heading of Level Three with starting hash
#### Heading of Level Three with starting hash and space
* list item
* list item spacey
* list item spaceous
=> http://example.org/no/name
=> http://example.org/with/name Linkname
>Quote Tight
> Quote Nice
>>Quote starting with gt
>>> Quote starting with two gts
End
cat example.gmi | python line.py
{'heads': '#', 'h3': None, 'h2': None, 'h1': '#', 'list': None, 'link': None, 'url': None, 'quote': None, 'content': 'Heading of Level One'}
{'heads': '##', 'h3': None, 'h2': '##', 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': None, 'content': 'Heading of Level Two'}
{'heads': '###', 'h3': '###', 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': None, 'content': 'Heading of Level Three'}
{'heads': '###', 'h3': '###', 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': None, 'content': 'Heading of Level Three Tight'}
{'heads': '###', 'h3': '###', 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': None, 'content': '#Heading of Level Three with starting hash'}
{'heads': '###', 'h3': '###', 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': None, 'content': '# Heading of Level Three with starting hash and space'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': '* ', 'link': None, 'url': None, 'quote': None, 'content': 'list item'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': '* ', 'link': None, 'url': None, 'quote': None, 'content': 'list item spacey'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': '* ', 'link': None, 'url': None, 'quote': None, 'content': 'list item spaceous'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': '=> http://example.org/no/name', 'url': 'http://example.org/no/name', 'quote': None, 'content': ''}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': '=> http://example.org/with/name', 'url': 'http://example.org/with/name', 'quote': None, 'content': 'Linkname'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': '>', 'content': 'Quote Tight'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': '>', 'content': 'Quote Nice'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': '>', 'content': '>Quote starting with gt'}
{'heads': None, 'h3': None, 'h2': None, 'h1': None, 'list': None, 'link': None, 'url': None, 'quote': '>', 'content': '>> Quote starting with two gts'}
Archive home | About this archive