Create a message of the day generator

閲覧: 159 回
最初の未読メッセージにスキップ

Melvin

未読、
2019/09/17 20:20:582019/09/17
To: tiddl...@googlegroups.com
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?

A Gloom

未読、
2019/09/17 20:49:312019/09/17
To: TiddlyWiki
I like it-- that I have a use for : )

TonyM

未読、
2019/09/17 20:59:502019/09/17
To: tiddl...@googlegroups.com
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

TonyM

未読、
2019/09/17 21:14:212019/09/17
To: TiddlyWiki
Minor edit

Melvin

未読、
2019/09/17 21:27:542019/09/17
To: TiddlyWiki
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;
};






})();

Birthe C

未読、
2019/09/17 22:21:482019/09/17
To: TiddlyWiki
Hi Melvin,

Thank you very much. The filter makes it very useful for an end user like me. I used it for my cookbook, which recipe to prepare each day the next month - no matter who in the family will be preparing the food, they will be linked to the correct recipe.


Birthe

Mohammad

未読、
2019/09/18 0:31:102019/09/18
To: TiddlyWiki
I believe this is possible with pure wikitext!

One solution:
I have 32 tiddlers each contains a message
Each tiddler have a field called day and message of day one has 1 as the day value
Write a list widget with a filter to extract the day number of current data
Match against field value of tiddlers has such field
Show the one match


Side note
I believe pure wikitext is a better way to write JS
Many of us understand wikitext while few understand JS

--Mohammad

Mat

未読、
2019/09/18 2:50:372019/09/18
To: TiddlyWiki
I made this, only to see that Tony is on the same path - i.e simply use the SetWidget select parameter. And AFAICT no need for a wikify outside.

<$set
name="messages" 
select=<<now DD>>
filter="
[[Message 1]] 
[[Message 2]]
[[Message 3]]
[[Message 4]]
...">
<<messages>>
</$set>

<:-)

TonyM

未読、
2019/09/18 5:47:022019/09/18
To: TiddlyWiki
Nice Mat

I love it when one persons idea is made even more elegant by another.

It is also a great source of code methods and patterns which are easier to learn than reverse engineering.

Thanks
Tony

TonyM

未読、
2019/09/18 5:48:432019/09/18
To: TiddlyWiki
Post script

Mat you method looks close to a "case" structure.

Love it
Tony

Melvin

未読、
2019/09/18 5:55:212019/09/18
To: tiddl...@googlegroups.com
Wow, that's beautiful :)


Op wo 18 sep. 2019 om 11:48 schreef TonyM <anthony...@gmail.com>:
--
You received this message because you are subscribed to a topic in the Google Groups "TiddlyWiki" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywiki/66Qpgtbv_-w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/b3ad9b79-e8eb-4478-bdec-291d9afc8d7d%40googlegroups.com.

TonyM

未読、
2019/09/18 6:02:362019/09/18
To: TiddlyWiki
Melvin,

Hood on you for taking the JavaScript challenge. I am sure it will help you and our community.

Of course you can see I mat and others always like a coding challenge and we can't help ourselves.

Tony

Mohammad

未読、
2019/09/18 7:51:152019/09/18
To: TiddlyWiki
\define tid() x$(item)$


<$set name="dd" value=<<now DD>> >
<$list filter="[tag[message]removeprefix[message ]match<dd>]" variable=item>
<$transclude tiddler=<<tid>> mode=block/>
</$list>
</
$set>

--assumption
  • message tiddler have title like message xx where xx is 1 2. ... 31
  • message tiddler have been tagged with message (this can be any other criteria)
When you open the the wiki or tiddler with daily message it will show message based on that day! For Sep 18 the `message 18` will be shown.

TonyM

未読、
2019/09/18 8:04:052019/09/18
To: TiddlyWiki
Mohammad

Yet another approach.

Love it
Tony

TonyM

未読、
2019/09/18 8:19:212019/09/18
To: TiddlyWiki
And..

\define msg-field() msg-<<now "DD">
\define msg-tiddler() tiddlername

then in any location
<$transclued tiddler=<<msg-tiddler>> field=<<msg-field>> />

store the messages in fields msg-1 msg-2 .... In tiddler name.

In this way you could even have alternative tiddlers with another set of messages for the day. Even 12 for different for every day of the year.

Regards
Tony

Mohammad

未読、
2019/09/18 8:44:322019/09/18
To: TiddlyWiki
Tony!
 This solution you proposed is even better! I may then suggest to use a simple data tiddler or JSON
stores all messages with property names msg-1 msg-2 or simply 1, 2, 3,...
wikitext allowed in property value in data tiddlers, so one can also style the text.

Thanks Tony for your great idea! Always you show new routes!

--Mohammad

Mohammad

未読、
2019/09/18 12:09:222019/09/18
To: TiddlyWiki

TonyM

未読、
2019/09/18 16:38:542019/09/18
To: TiddlyWiki
Mohammad,

Good idea with the data tiddler as well. 

So many ways in tiddlywiki.

Regards
Tony
全員に返信
投稿者に返信
転送
新着メール 0 件