Hey Tony,

I suppose to each their own :P

I just learned how to create JS macro's. I finally understand enough of TW 
to have an idea of what that even means. The csvtiddlers.js file was really 
helpful to see how to deal with filter expressions in JS. Also, while 
reading the source is a bit 'scary' since the concepts are still quite 
vague to me (and I have no clue how the parse tree or rendering works), 
large parts of it are almost immediately understandable.

In any case, the macro is callable as such:
 <<messageoftheday "[[somefilter expression]]">>

Example

<h1><span class="message-of-the-day">Message of the day: 
<<messageoftheday "


[[Message 1]]


[[Message 2]]


[[Message 3]]


[[Message 4]]


">>
</span></h1>

The JavaScript code is as follows (obviously I'd advise you to change the 
"melvin" part of the pathname, or keep it as is at your own risk ;-) ):

/*\
title: $:/melvin/macros/messageoftheday.js
type: application/javascript
module-type: macro
Macro to output a message of the day from a filter
\*/
(function(){


/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";


/*
Information about this macro
*/


exports.name = "messageoftheday";


exports.params = [
  {name: "filter"},
];


/*
Run the macro
*/
exports.run = function(filter) {
  var defaultFilter = "[[the default message has arrived]]";
  filter = filter || defaultFilter;
  var messages = this.wiki.filterTiddlers(filter);
  var adjustedDate = new Date().getDate() - 1;
  var messagesIndex = adjustedDate % messages.length;
  var result = messages[messagesIndex];
  var debug = [" ", result, messagesIndex, adjustedDate].join(" "); // 
concatenate the debug variable to result if you want to see any variables 
of interest
  return result;
};






})();


On Wednesday, September 18, 2019 at 3:14:21 AM UTC+2, TonyM wrote:
>
> Minor edit
>
> On Wednesday, September 18, 2019 at 10:59:50 AM UTC+10, TonyM wrote:
>>
>> Melvin,
>>
>> You could mimic the Javascript approach quite closely, Because my 
>> Javascript is somewhat vague and it is more involved to add it to 
>> tiddlywiki I always use other methods.
>>
>> \define list-of-messages() [[Message 1]] [[Message 2]] [[Message 3]] [[
>> Message 4]]
>>
>> or even
>> \define list-of-messages() 
>> [[Message 1]]
>> [[Message 2]]
>> [[Message 3]]
>> [[Message 4]]
>> \end
>>
>> And then you can use the set widgets select attribute to choose which 
>> message to display. Since it is zero based use the new maths operators to 
>> subtract one.
>> Maths operators work nicely inside the triple braces.
>>
>> So *Untested* example
>> <code>
>> <$set name=dd value=<<now DD>> ><!-- I did not check if this works -->
>> <$set name=todays-message filter=<<list-of-messages>> select={{{ [<dd>
>> subtract[1]] }}}>
>> <<todays-message>>
>> </$set></$set>
>> </code>
>>
>> Regards
>> Tony
>>
>> On Wednesday, September 18, 2019 at 10:20:58 AM UTC+10, Melvin wrote:
>>>
>>> This took me a while, but I found a way to create a message of the day 
>>> generator which changes the message given on how many messages you have and 
>>> what day of the month it is. 
>>>
>>> You add the messages in [[]] and the code will do the rest :D
>>>
>>> The current 'bug' is that it will only allow you to have about 30 
>>> messages, as it cannot display more messages of the day than there are days 
>>> in the month.
>>>
>>> Copy/paste this Tiddler and see what message of the day you get!
>>>
>>> <style>
>>> .message-of-the-day {
>>>   color: red;
>>> }
>>> </style>
>>>
>>> <$wikify mode="inline" name="dayofmonth_val" text=<<now DD>> >
>>> <$set name="messages" filter="
>>>
>>>
>>> [[Message 1]] 
>>>
>>>
>>> [[Message 2]]
>>>
>>>
>>> [[Message 3]]
>>>
>>>
>>> [[Message 4]]
>>>
>>>
>>> ">
>>> <$set name="list_length" filter="[enlist<messages>count[]]">
>>> <$set name="list_index" 
>>> filter="[<dayofmonth_val>remainder<list_length>]">
>>> <$list variable="result" filter="
>>> [enlist<messages>nth<list_index>]">
>>> <h1><span class="message-of-the-day">Message of the day: 
>>> <<result>></span></h1>
>>> </$list>
>>> </$set>
>>> </$set>
>>> </$set>
>>> </$wikify>
>>>
>>>
>>> My question here is: is there any way to do this more efficient?
>>> In JavaScript this is the equivalent of: 
>>>
>>> var messages = ["Message 1", "Message 2", "Message 3", "Message 4"]
>>> var result = messages[new Date().getDate() % messages.length]
>>> //and then display the result var in anyway you see fit
>>>
>>> Which would be so much easier to write.
>>>
>>> I guess I need to learn how to create JS macro's or something similar.
>>>
>>> What do you think?
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/83917f08-97e9-4cd5-9df3-a5ca5e853646%40googlegroups.com.

Reply via email to