Creating a Blog page inside MODX version 3 web site

MODX comes clean slate so there is no ‘ready to use’ provisions for creating Blog style site. One has to set it up using tools available in the CMS.

I took an easy way out for my production site by creating blog site using free wordPress service and then feed content to MODX page. with this, the only extra you will need is Spiefeed to handle rss feed from WordPress.  But for purist approach it is certainly possible to setup blog section within MODX using few extras.

There are several articles available for creating Blog sites using MODX.  The one shown in the modx official site is somewhat dated, but the article actually have a link to the instruction/article of more recent implementation of MODX blog site.  Link is here.   Better yet, this instruction is accompanied with a package published in the github which has all chunks, a snippet and a template packaged up to duplicate the blog site that the article discusses.  A demo site is also available to check out the feel of using this template. The document states that the demo utilizes MODX 2.7.  So can I use this setup with MODX 3 site? Furthermore, the instruction assumes you want the Site to be a blogging site. Can I add ‘Blog page’ to already existing MODX site? Answer to both questions are ‘Yes’ but we need to modify some parts.

To start, I could have created each chunk and template manually following the instruction but to reduce my typing effort, I went ahead and downloaded the demo package. I also installed dependent extras (getResources, getPage, Collections, and Tagger).  Previously, I did not have any of those installed. – I am using pdoTools  to get resources and get page. – <edit> It was fairly easy to change references to getResources and getPage to equivalent pdo* snippets.

Again, the setup instruction is written for MODX version 2.7 and the site I am setting up this blog structure is version 3.0.3.  Setup also assumes the top page is a site home page. There are several area in chunks and template that refers to [[++site_start]] and this need to be changed to point to correct Resource.  I want to have a blog page as a subpage of the site.

Looking through the template and snippets that I setup with the downloaded package , I found some more modifications were needed.

I will list out those changes.

  1. My server uses Nginx and it needs additional html tag to specify base url in <head> section of blog template html. Without it, Friendly URL won’t work correctly.
    <base href="[[++site_url]]">
  2. In MODX 3, [[*class_key]] does not return “modResource”.  It returns “MODX\Revolution\modResource”.
    This means I get “MODX/Revolution\modDocument” or “MODX\Revolution\CollectionContainer” in [[*class_key]] field depending on resource selected. Two sections of “Blog Template” stops working due to this.

    1. Template tries to pull in “jumbotron_CollectionContainer” or “jumbotron chunk based on [[$jumbotron-[[]*class_key]]] tag. This tag doesn’t work in MODX 3 as it returns [[$jumbotron-MODX/Revolution/….]] which does not exist and forward slash within chunk name is probably invalid anyway.
    2. Template tries to insert [[$blog_listing]] or [[$blog_article]] based on conditional modifier [[*id:is`[[++site_start]]`:then….]] but the blog_listing page is not at [[+site_start]] resources in my use case.
      1. To mitigate both problems, I have created a system setting [[++blog_start]] in custom namespace/area and set it to id of a blog home page resource I created. Then rewrote tags as follows.
        For jumbotron section
        [[[[*id:is=`[[++blog_start]]`:then=`$jumbotron-CollectionContainer`:else`$jumbotron-modDocument`]]]]
        For container section
        [[[[*id:is=`[[++blog_start]]`:then=`$blog_listing`:else=`$blog_article`]]]]
      2. Note: It will be probably not that difficult to create snippets and replace conditional tags here.
  3. getResources and getSnippet tags are used to aggregate child resources and parents properties are set to site home page [[++site_start]].   These needed to be changed to aforementioned [[++blog_start]].  These changes had to be made on $blog_listing chunk and $jumbotron-CollectionContainer chunk.
  4. @groups properties are hardcoded in calls to TaggerGetTags snippet.  The snippet is called from $aside chunk is setting @groups to 1 and the snippet called from $headder chunk is setting @groups to 2.  Make sure this agrees with your tagger group settings. I have first created ‘Category’ group and then ‘Archives’ group so I needed to change them to 2 and 1 respectively.
  5. Last and least 🙂   as this is just a demo template, links to “subscribe”, “search”, “Sign up”, “GitHub”, “Twitter” and “Facebook” are all mocked and pointing to itself.  You either need to remove or put actual link to them when modifying to suit your site design.

So, in order to create a Blog section within Modx 3 hosted website, using this instruction and elements in the github repo, One needs to be aware of:

  1. Modx version 3’s breaking change of the model class caused decision logics inside Blog_Template to stop working.  Rework of the logic flow was needed.
  2. Friendly url.  Nginx needs base href reference in head section.
  3. Creating a blog section other than home page location. Needed to change references to [[++site_start]] system settings to [[++blog_start]], a user created system setting
  4. Adjust the template design to fit with other area of site design.

Some more miscellaneous items to note.

  1. $blog_listing_item chunk, content placeholder has a modifier :firstp. There is a firstp snippet that is installed with the package.  I opted to replace it with ellipsis=`500` modifier to limit the content output to first 500 chars followed by ellipsis to suit my preference.
  2. Also in $blog_listing, getPage snippet uses @blog properties set. This set does not exists so the default property set will be used instead. I think idea is to create a blog property set within getPage snippet to override the default property settings in easy way, if customization is needed.

Commenting on the posts are not possible.  This need to be setup by yourself and will require Quip extra. that part is not covered by this article. One has to go back to modx documentation’s tutorial section.

 

factory function?

JavaScriptやPHPで、Function Factoryという言葉を見るのだが、これは共通するパターンのFunctionを生成するためのFunctionということで、例えば


$double = function($x){
    return $x*2;
};

$triple = function($x){
    return $x*3;
};

$quadruple = function($x){
     return $x*4;
};

なんてのは、三つのfunctionを定義していても、よく見ると、2,3,4とかける数字が違うだけで、あとはまったく同じパターン。 このようなfunctionの記述を効率よく行うために

というような書き方をするテクニック。肝心な部分の記述は一回で済むためコードの管理も簡単になる。

上のコードは返すコードのなかで、$xを認識させるため、 use ( $x )というクローズをいれているが、PHP7.4 からPHPでもアローファンクションが使えるようになり、これだとuse というkeyword無しで、外側で設定されている変数を認識するようになる。ので、

と、ファクトリーの部分の記述がすっきりする。

さらにこの上位のファンクションもアローファンクションで書いてしまうと

となり、 プロのコーディングでアローが二つも三つもかさなるような記述が時々でてくるのだが、そろそろ読解がしんどくなってくるので、後で読み返してわかる記述方法としては、よし悪しかなあ。

Week number confusion

Just encountered this problem when I was talking with my colleague.  He thought this week was week 43 but it actually was 42 on my calendar.  Turned out, there are two ways to look at week number.

First one is to simply take January first as a start of week one.  In case of 2021, January 1 is Friday.   depending of what you think as a first day of week, this year’s week 1 is only 2 days long or 3 days long, then Week 2 will start on either January 3rd(Sunday) or January 4th (Monday)

Excel function =WeekNum()  uses this approach.

Then there is a week number definition by ISO 8601.  According to ISO,  Week 1 is the first week of the year that has more than 4 days in a week.  Further more, first day of week will start on Monday.

With this definition, January first of 2021 does not qualify as a  week number 1.  A week starts on January 4th(Monday) is week number 1.

Excel also has function to calculate this as   =ISOWeekNum()

When I first turn on week number display on Outlook,  it showed the week number based on ‘January 1 as week 1’ approach.   This is not correct on many occasion. It appears majority of business now uses ISO week number.   You can change the settings in Outlook calendar to correct this.

In Outlook, go to File then select ‘Option’ then select ‘Calendar’  In Worktime  section, change ‘First day of week’ to ‘Monday’ then First Week of Year from ‘Starts on Jan 1’ to ‘First 4 day week’

Trying to code ISO weeknumber in typescript turned out to be a fun project but that is another story..

Layoffされていて思ったこと

実は去年の4月中旬から7月中旬までレイオフされていた。会社によってはFurloughといったりするが、要は一時解雇。 給料は出ない。保険も一部カバーされなくなる。その代わり、と言っちゃなんだが 失業保険をもらえる’権利’が生ずる。 現在の米国の失業保険は週に360ドルだが、 Covid-19が猛威を振るい始めた段階で、4月から7月にかけては一時的に週に600ドルの特別手当がつくようになった。 なので、週にもらえる手当が960ドルだ。月に換算すると4,114ドル、日本円に換算すると約43万円。自分の場合はすでに子供は自立し、家の支払いも終わっているから、これだけ支給されれば生活にはこまらなかったが、 色々と考えることがあった。

1)退職の時期。 自分は日本にいたらすでに退職の年齢に達している。アメリカの場合は退職のタイミングは自分で決めるので、年金を受け取らないことによるペナルティが発生する71歳ぎりぎりまで働くことは可能なのだ。 とは言ってもそこまで働くつもりはあまりなく、 今開発中の製品が生産に移った時点で辞めようと考えていたのだが、 今回仕事をせずに過ごしてみて、少し考えが変わった。 色々習い事をしたり、趣味に時間が取れるのはうれしいのだが、なんともいえないすかすか感が漂う。 まだ引退するには早いかなあ、と思ってしまったのは事実。

2)自分の働く会社は一時キャッシュフローが全くの0になってしまったので、従業員の数を25%くらいにまで落とした。 2か月ほどで50パーセントまで戻したが自分はその勘定に入っていない。職種を考えると当たり前なのだが、給料がもらえないということより、自分が必要とされていないという疎外感のほうが精神的にきつかった。

というわけで退職後の生活を本当にどうすれば気持ちの健康を保てるのか、ということを考える良い機会になった。

 

ありがたいことに米国人の上司が自分の年季はあと一年くらいしかないということを知りながら、呼び戻してくれた。

最終的に呼び戻されたのは全従業員の2/3で、 残りは解雇。 それから3か月たったが仕事の流れに支障がないといえばウソになる。自動車業界というのは恐ろしいところで、2か月間生産がストップしたため市場在庫が底をつき、これを埋める形で生産の量はほぼ通常に戻ってしまったどころか、あちこちで部品の欠品が発生し、今や生産を維持するために毎日が火消し状態。単純に考えても仕事の量が50パーセント増えたのに、これだ。 えらいことである、

と 書いたのが1年前、 そのころは2021年の夏も過ぎればこの状況収まっているのだろうと思っていたのだがこれがとんでもない誤算。

まずコロナ騒ぎがおさまっていない。アメリカにしてもワクチンの接種率が頭打ちになってしまった。ワクチンに不安を持った層を共和党が煽った結果だが、アメリカという国民は科学や合理性を信じていない輩が半分ちかくいる、これが簡単に騙される、ということを改めて痛感した。 FaceBook/Twitter/youtube などにあふれているFake Newsにいとも簡単に乗せられている。これらのSNSは似たような記事をお勧めに載せるようにできているので、嘘でもどんどん信じるようにできているようだ。

それはともかく。

部品欠品の状況が一向に収まらない。これはアジアの生産拠点などが順繰りにコロナで閉鎖されたりすることによってSupply Chainが滞ってしまっていることが原因だが、それにより、各自動車メーカーが減産を余技なくされている。

自動車は何千点の部品で構成されているので一つでも欠品すると自動車として完成しないのだ。 困ったことに、全体的に減産しているので余っている部品は在庫がふくらみ、業者にとっては踏んだり蹴ったりである。

 

Hyperbolic function on Casio fx-991EX

There is no dedicated HYP key to call up cosh() or sinh() function in Casio fx-991EX.   This is a stern contrast to Casio fx-115ES keyboard.  It does not mean you cant use the hyperbolic funtction on fx-991EX

Hit “OPTNOption” key then select “Hyperbolic Func”

You need to be in “Calculate” mode to see this option, so for repeatability,

Hit menu, select 1.   Hit OPTNOption, select 1.

This calculator is obviously not RPN.  The calculator uses “natural notation” format.  It basically let you create equation and then evaluate the answer.  No concern on intermediate calculation.  Just answers, and it is really a wonder that you can get those devices for less than $20.

DM42

自分が使っている計算機はいまだにRPNである。 いまや科学技術計算はもとより、日ごろの営業計算もコンピューター上のエクセルで行ったほうがよっぽど早いので、専用の卓上計算機は 机上のアクセサリーであるとしか言えず、 たまに使うときも 製品単価のマージンをみるときぐらい。 そう %キー Δ%キーを押すほうがエクセル上で計算式をタイプするより簡単な時ぐらいである。 老後のマネービルディングを考えるのにも必要な機能は金融計算。 そういうことで、ビジネス計算機のHP-30bで事足りる。

ともあれ、Sine/Cosineをはじめとした三角関数 ルートやべき乗の関数。や数百ステップのプログラム機能を持った科学計算電卓というのはパソコン黎明期以前にエンジニアになった身としては特有な感慨がある。 これだけならばCasioのfx計算機を一台持っていればよいことになる(去年、Casio fx-991exという学校教材用の電卓をTargetというスーパーマーケットで15ドルで購入し、コスパの高さに驚愕した。)が、RPNという計算法を一度肌で覚えてしまうとどうしても計算機はHPがいいよね。ということになる。

専用のアルファキーがついているので、便利、この部分は完全に裏側に折りたためるので普通の計算機のように片手で持って操作していた。

自分として最初のHPはHP-28Sという、手帳のように折りたためるRPL機だったが、これはフレキシブルケーブルがダメになったか、アルファ側のキーが反応しなくなったので、その後、やはりRPLを実装した48Gを購入。48Gはいまだに動作するが、さすがに動作速度がおそく、Android AppのDroid48とかEmu48はグラフィックを一瞬で描画するが、元祖のほうの描画速度はとてつもなく遅い。 どちらにせよ、28にしても48にしても自分が必要とする機能は100パーセント以上満たしている。

これで終わっていればよかったのだが、 HPの計算機というのはコレクター意欲をそそる側面があるようで、その後 趣味の世界に突入し HP-33S,HP-35S、HP-12C, HP-15C LE、HP-30b とRPN機への散財が続くようになった。 さらに オタク化し、金融計算機のhp20-b/30bをフラッシュしなおしてwp-34sという科学計算機を”作成”して使用中。

WP34Sは自作するか、カスタムメードしてくれる個人からの購入になるが、マニュアルは製本されたものがAmazonで購入可能

このwp-34S 、キーに割り付けられた機能が多いわりには非常に使いやすいのだが、コンセプトのベースとなったのがHP-42sであり、42sを使いなれた人には操作でまごつくことはないはず、という但し書きがついている。

42sはHPのフラッグシップでNASAと一緒に宇宙まで行った41Cの後継機だが、、モジュールプラグインなどの拡張機能がないので41シリーズの影に隠れて存在感が薄い。 ただし、その後Free42というオープンソースプロジェクトにてアプリとして復活し、いまやWindowsはもちろん、スマホやタブレットなどでも使うことが可能。

Free42ですごいのはこれが単なるロムのエミューレーションではなく、一から書き直されているところ。そして、プログラムのロード・セーブがエミュレーターのプラットフォーム上でできるため、42sの弱点であった、プログラムライブラリーの利用が手入力でしかできないという欠点が解消されている。41cとは開示されている仕様内ではプログラム言語が上位互換なので、41C/41CV/41CXのプログラムライブラリーがほとんど使えるということだ。

で、そのFree42をArmコアでハードウエア化してしまったのが、SwissMicros社から発売されているDM42である。

値段は安くはない。今時卓上計算機に$200ドル以上払うのはコレクター以外にはいないんだろうと思えるのだが、(自分もその一人) 今回コロナウイルスの影響で一時解雇されていたわが身が職場復帰できたのを一人で勝手に祝って購入してみた機体のシリアル番号は6000番台である。 この計算機は2017年に発売開始だから、年間2000台は売れていることになる。

ちなみにSwiss Mailで配送され、北米のミシガン州にはオーダーしてから2週間以内で届いた。

 

使い勝手は非常に良い。 大きなメモリーグラフィック液晶の表示も見やすいし、ボタンのクリック感もCasioやシャープとは一線を画し、HPの計算機に近い感じ。 Youtubeを探すとすでに何件も動画があがっているようで参考にしてみてほしい。 動画の一部で言っているキーボードの不具合はすでに解決されていた。 USBを使ってのFirmWareの書き換えも可能。

Logan West氏によるHP-42SとDM42の比較動画

昨日、SwissMicrosからお知らせが来て、同じハードウエアで41xを発売開始したとある。こちらはオリジナルROMのイメージを使ったEmulatorなので、41Cシリーズとはマイクロコードレベルでの互換性がある。しかもExpansion Moduleはすべて格納済みだそうである。  コレクションにもう一台増えそうだな、と思っている。

coronavirusと生活への影響

自分の住んでいるミシガンも3月23日から緊急事態宣言が発令され、不要不急の用事を除いては外出をひかえるような指示が出ている。

その前の週から、勤めている会社ではすでに自主的に在宅勤務の指示が出ていたわけで、州知事からStay Home Directiveが発令されたときには家からテレコミュートしていた。。

1.飲食店は営業停止。ただし持ち帰りと出前は続行。
2.ガソリンスタンド、銀行、郵便局などは営業継続
3.スーパーや食料品店は営業継続
4.病院関係は通常通り

というわけで食料品などの買い出しに出ても、当初あったようなトイレットペーパーなどの品不足は解消されているように見える。

酒屋がオープンしていたのには笑った。必要不可欠な部類に入っているのだなあ。

ミシガン州での確認された患者数は3月30日の時点で 5486名。なくなった方は132人。

なくなった方は男性のほうが圧倒的に多い。

30歳以下は羅漢率も死亡率も低い。 死亡者の平均年齢が自分の歳と重なる、というのも嫌なものです。

 

 

Percentage of Cases by Age
  Age %
  0 to 19 years 1%
  20 to 29 years 9%
  30 to 39 years 13%
  40 to 49 years 17%
  50 to 59 years 20%
  60 to 69 years 19%
  70 to 79 years 14%
  80+ years 8%

 

Overall Percentage of Deceased by Sex

  Sex %
  Male 69%
  Female 30%
  Unknown 1%

 

 

Age Data of Overall Deceased
  Average Age 64.4 years
  Median Age 65 years
  Age Range 25-97 years

 

3月23日外出停止命令が出て1週間たった。 新規の患者の数が初めて減少に転じたようだが、外出停止の効果が出始めたのか、ただの天の気まぐれか。 どちらにしろこの傾向が続くことを祈るのみです。

 

 

Python Generator

>>> def make_counter(x):
	print('entering make_counter')
	while True:
		yield x
		print('incrementing x')
		x = x + 1

		
>>> counter = make_counter(2)
>>> counter

>>> next(counter)
entering make_counter
2
>>> next(counter)
incrementing x
3
>>> next(counter)
incrementing x
4
>>> next(counter)
incrementing x
5
>>> def fib(max):
	a,b = 0,1
	while a < max: yield a a,b = b, a + b >>> for n in fib(1000):
	print(n,end = ' ')

	
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
>>> list(fib(1000))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>> 

Python Generator の話

K.Yairi のVintage Guitar

1981年の春ごろだと思うからもう40年近く前の話になる。 初めて米国の土を踏み、 2か月ほど働いて最初の給料が払われ、当時の金額で大枚500ドルを(為替レートが一ドル220円くらいでしたな)握りしめ真っ先に向かったのは前から目を付けていた町のギターショップだった。  あまり高くなくてもいいからMartinのDシリーズのボディーのギターを購入するつもりだった。  ところが店のマスターは「売らんでもないが、マーチンなんか買うな。」という  350ドルで勧められたのは日本製のギターの K.Yairi。 当時 S.Yairi と K.Yairi という二種類のYairiががあるというくらいは知っていたのだが、 日本にいたころはやはりMartinとかGibsonとかコンポジット材を使ったObationなんかが有名で、アメリカに行ったらアメリカ製を買うつもりだったのに、「悪いことは言わないから、値段も一回り安いし、この日本製を買いなさい。」と言われてしまったわけだ。

というわけで諭されてK.Yairi(Alvarez)を購入した。 時は過ぎ、結婚して子供ができ、その息子が大きくなってギターを弾きたいと言って地下室にしまってあったYairiをひっぱりだしてきた。10年くらいは触ってもいなかったわけで Tune UPに出すように言って、彼がGuitar Centerという最寄りのChain店に持って行った。 息子曰く、 Techが「お、Yairiだ」と言ってfHoleの中を望みこみ、 顔を見上げて、「兄ちゃんこのギターどこで手に入れた? これは掘り出しものだぜ」 と言ったそうな。

預かり証に書き込んてくれた時価評価額が3,900 USドル ずいぶんと中途半端な値段だが、これを聞いた親のほうが驚いた。 息子はホクホクものである。 ギターは初心者のはずだったが、さすがは鈴木チルドレン。 コードも難なく覚え、すでに自分より手練れの様子である。 さすがです。