Improving formats conversion.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2021-06-06 16:07:41 -05:00
parent e8c73c7563
commit 5cc4d7cfa4
2 changed files with 60 additions and 0 deletions

View File

@ -32,6 +32,11 @@ Tiddler >> asDictionary [
yourself.
]
{ #category : #converting }
Tiddler >> asJson [
^ STON toStringPretty: self asDictionary
]
{ #category : #accessing }
Tiddler >> caption [
@ -101,6 +106,20 @@ Tiddler >> list: anObject [
list := anObject
]
{ #category : #utilities }
Tiddler >> markdownLinksAsWikiText [
"I'm useful to convert _internal_ links between formats, as is a common pattern
found when migrating content from Markdown to TiddlyWiki's WikiText.
I DON'T work on external links. A better regex could be used for that.
See:
- https://davidwells.io/snippets/regex-match-markdown-links
- http://blog.michaelperrin.fr/2019/02/04/advanced-regular-expressions/"
| markdownLinks |
markdownLinks := (self text splitOn: Character space) select: [:each | each matchesRegex: '\[(.+)\)'].
markdownLinks ifEmpty: [^ self].
^ markdownLinks
]
{ #category : #accessing }
Tiddler >> modified [
@ -125,6 +144,13 @@ Tiddler >> mofier: anObject [
mofier := anObject
]
{ #category : #accessing }
Tiddler >> printOn: aStream [
super printOn: aStream.
aStream
nextPutAll: '( ', self title, ' )'
]
{ #category : #accessing }
Tiddler >> tags [

View File

@ -0,0 +1,34 @@
Class {
#name : #WikiText,
#superclass : #Object,
#instVars : [
'content'
],
#category : #'TiddlyWiki-Model'
}
{ #category : #accessing }
WikiText >> content [
^ content
]
{ #category : #accessing }
WikiText >> content: aString [
content := aString
]
{ #category : #accessing }
WikiText >> convertMarkdownLinks [
| markdownLinks markdownLinksRegex |
markdownLinksRegex := '\[([\w|\s]+)\]\((\S+)\)'.
"For the explanation of the Regex details see: http://scottradcliff.com/how-to-parse-urls-in-markdown.html"
markdownLinks := self content regex: markdownLinksRegex matchesCollect: [:link | link ].
markdownLinks ifEmpty: [^ self content ].
markdownLinks do: [:markdownLink | | linkText closingLinkIndex newContent |
closingLinkIndex := markdownLink indexOf: $].
linkText := markdownLink copyFrom: 2 to: closingLinkIndex.
newContent := self content copyReplaceAll: markdownLink with: '[[', linkText, ']'.
self content: newContent.
].
^ self content
]