use ELSE run filter prefix and remove surrounding single or double quotes

71 views
Skip to first unread message

Mohammad

unread,
Dec 20, 2018, 1:59:36 AM12/20/18
to TiddlyWiki


Using the ELSE run filter prefix

The below code is a demo of ELSE run ~ filter prefix. It's purpose here is to get a parameter and remove its surrounding single or souble quotes. If the parameter has not any surrounding quotes, then returns itself

Macro

\define remove-quotes(term)
<$set name="item" value=<<__term__>> >
  <$set name="noquote"
    value={{{ [<item>removeprefix["]removesuffix["]] [<item>removeprefix[']removesuffix[']] ~[<item>] }}}
    emptyValue="Process failed!"
  </$set>
</$set>
\end

Examples

Remove single quotes

><<remove-quotes "'single quotes'">>

That renders as:

single quotes

Remove double quotes

><<remove-quotes '"double quotes"'>>

That renders as:

double quotes

Empty value

The macro will return the Process failed! if no parameter passed

><<remove-quotes>>

That renders as:

Process failed








Question:
 Is there any better solution to do the above task? One application of this code is scanning a tiddler and finds occurrences of <<abbr "term">> where term is glossary index and you want to make a list of abbreviations at the bottom of your tiddler.

TonyM

unread,
Dec 20, 2018, 7:52:09 AM12/20/18
to TiddlyWiki
Mohammad,

Looks like it does the job.

Is the else actualy needed?

Rather than fail you could use the current tiddler if the input is blank.

Good to share!

Tony

Mohammad

unread,
Dec 20, 2018, 8:19:26 AM12/20/18
to TiddlyWiki
Hi Tony!
 I was looking to see if there is a shorter and more efficient solution.

The Else is used here to handle two cases of failure: if the input is empty! and if the input has no surrounding quotes!

-Mohammad

 

TonyM

unread,
Dec 20, 2018, 6:30:56 PM12/20/18
to TiddlyWiki
Mohammad,

I am at my desk this time, 

I am not sure why you are doing this? as in this case you are passing to the macro an intentionally quoted string? In some ways a minimal solution would be

\define removequotes(item)
<<__item__>>
\end

<<removequotes 'single quotes'>>
<<removequotes "double quotes">>


I have the following working which defaults to currentTiddler rather than throwing an error. Except it fails if current tiddler has double Quotes
\define remove-quotes(item)
<$set name="item" value=<<__item__>> emptyValue="""$(currentTiddler)$""" >
 
<$list filter="""[<item>removeprefix["]removesuffix["]] [<item>removeprefix[']removesuffix[']]"""></$list>
</$set>
\end

<<remove-quotes "'single quotes'">>
<<remove-quotes '"double quotes"'>>
<<remove-quotes>>


Given your solution, how are you planning to use it? What is the source of the titles with Quotes in their name?.

Regards
Tony

Mohammad

unread,
Dec 21, 2018, 12:17:24 AM12/21/18
to TiddlyWiki
Hello Tony,
 Actually my code is part a bigger code, in that code a quoted string is passed to be looked up in a dictionary tiddler and having those quotes cause problem.
I need to have an un-quoted key! One way is to use the first solution you proposed, the other is what I sent here!

I thought the second solution is better as it make sense and is the semantic way of doing this!

--Mohammad


TonyM

unread,
Dec 21, 2018, 2:40:07 AM12/21/18
to TiddlyWiki
Mohammad,

The use of Quotes is common, and they are removed by a number of processes, and can be implied. If you wanted a minimal way, perhaps this is it.

\define remove-quotes(item) <$set name=string value=$item$><<string>></$set>



<<remove-quotes "'single quotes'">>
<<remove-quotes '"double quotes"'>>
<<remove-quotes 'single quotes'>>
<<remove-quotes "double quotes">>
<<remove-quotes single-quotes>>
<<remove-quotes double quotes>>
<<remove-quotes """double quotes""">>
<<remove-quotes ""double quotes"">>

Try the above and observe which work, but your test cases do work

Tony

Mohammad

unread,
Dec 21, 2018, 3:34:57 AM12/21/18
to TiddlyWiki
Hello Tony!
 Brilliant!

Thank you


Reply all
Reply to author
Forward
0 new messages