diff --git a/fixtures/www.moongift.jp/1554559177617.html b/fixtures/www.moongift.jp/1554559177617.html new file mode 100644 index 000000000..280dcf4cf --- /dev/null +++ b/fixtures/www.moongift.jp/1554559177617.html @@ -0,0 +1,614 @@ + + + + + wasm-module - RustのWebAssemblyの中でDOMを扱う MOONGIFT + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

ITエンジニア/デザイナ向けにオープンソースを毎日紹介

+ +
+
+ + +
+
+
+ +
+ + +
+

個人的にWebAssemblyには強く期待しており、その中でもGoが有力だと感じています。それはGoのWebAssemblyではDOMやJavaScript APIが使えるからです。駆使すればWebアプリケーション全体のコードをGoで書けるのです。これがRustにもないのが残念でした。

+ +

しかしwasm-moduleが新しい可能性を見せてくれます。wasm-moduleはRustでもDOM操作を可能にしてくれるのです。

+ +

wasm-moduleの使い方

+ +

コンソールにメッセージを出すデモ。

+ +

+ +

コードはこのようになります。

+
extern "C" {
+    fn console_log(start: i32);
+}
+
+fn cstr(s:&str) -> i32{
+    std::ffi::CString::new(s).unwrap().into_raw() as i32
+}
+
+#[no_mangle]
+pub fn main() -> () {
+    unsafe {
+        console_log(cstr("hello world!"));
+    }
+}
+
+

さらにCanvasを使うことも。

+ +

+ +

入力値だって扱えます。

+ +

+ +

wasm-moduleは独自のタグ

<wasm -module></wasm>
を使ってWebAssemblyファイルを読み込みます。それによってライブラリを読み込み、WebAssembly内でDOMを使えるようになります。

+
extern "C" {
+    fn global_getWindow() -> i32;
+    fn Window_get_document(window:i32) -> i32;
+    fn Document_querySelector(document:i32,query:i32) -> i32;
+    fn HTMLCanvasElement_getContext(element:i32,context:i32) -> i32;
+    fn CanvasRenderingContext2D_fillText(canvas:i32,msg:i32,x:i32,y:i32);
+}
+
+fn cstr(s:&str) -> i32{
+    std::ffi::CString::new(s).unwrap().into_raw() as i32
+}
+
+#[no_mangle]
+pub fn main() -> () {
+    unsafe {
+        let w = global_getWindow();
+        let d = Window_get_document(w);
+        let canvas = Document_querySelector(d,cstr("#screen"));
+        let ctx = HTMLCanvasElement_getContext(canvas,cstr("2d"));
+        CanvasRenderingContext2D_fillText(ctx,cstr("hello world!"),50,50);
+    }
+}
+
+

コードはちょっと取っつきが悪そうな気はしますが、wasm-moduleのラッパーも登場するのではないでしょうか。そうすれば状況は大きく変わって、RustでWebアプリケーションを書く流れすらできそうです。

+ +

wasm-moduleはRust製のオープンソース・ソフトウェア(MIT License)です。

+ +

richardanaya/wasm-module: write web assembly that uses that dom easily

+ + + +
 
+ +
+ +
+
    +
+
+
+
+
+

MOONGIFTの関連記事

+ +
+
+   +
+ +
+
+
+

コメント

+ +

+ + +

+
+ + +
+
+

+ + +

+
    +
  • DevRel
  • +
  • Com2
  • +
+
+ + +
+
+ + +
+ + + + + + + + + + diff --git a/src/extractors/custom/index.js b/src/extractors/custom/index.js index c2424ad0e..a9cce3b62 100644 --- a/src/extractors/custom/index.js +++ b/src/extractors/custom/index.js @@ -107,3 +107,4 @@ export * from './genius.com'; export * from './www.jnsa.org'; export * from './phpspot.org'; export * from './www.infoq.com'; +export * from './www.moongift.jp'; diff --git a/src/extractors/custom/www.moongift.jp/index.js b/src/extractors/custom/www.moongift.jp/index.js new file mode 100644 index 000000000..6ec35dbea --- /dev/null +++ b/src/extractors/custom/www.moongift.jp/index.js @@ -0,0 +1,30 @@ +export const WwwMoongiftJpExtractor = { + domain: 'www.moongift.jp', + + title: { + selectors: ['h1.title a'], + }, + + author: null, + + date_published: { + selectors: ['ul.meta li:not(.social):first-of-type'], + timezone: 'Asia/Tokyo', + }, + + dek: { + selectors: [['meta[name="og:description"]', 'value']], + }, + + lead_image_url: { + selectors: [['meta[name="og:image"]', 'value']], + }, + + content: { + selectors: ['#main'], + + transforms: {}, + + clean: ['ul.mg_service.cf'], + }, +}; diff --git a/src/extractors/custom/www.moongift.jp/index.test.js b/src/extractors/custom/www.moongift.jp/index.test.js new file mode 100644 index 000000000..5f651ee02 --- /dev/null +++ b/src/extractors/custom/www.moongift.jp/index.test.js @@ -0,0 +1,115 @@ +import assert from 'assert'; +import URL from 'url'; +import cheerio from 'cheerio'; + +import Mercury from 'mercury'; +import getExtractor from 'extractors/get-extractor'; +import { excerptContent } from 'utils/text'; + +const fs = require('fs'); + +describe('WwwMoongiftJpExtractor', () => { + describe('initial test case', () => { + let result; + let url; + beforeAll(() => { + url = + 'http://www.moongift.jp/2019/04/wasm-module-rust%e3%81%aewebassembly%e3%81%ae%e4%b8%ad%e3%81%a7dom%e3%82%92%e6%89%b1%e3%81%86/'; + const html = fs.readFileSync( + './fixtures/www.moongift.jp/1554559177617.html' + ); + result = Mercury.parse(url, { + html, + fallback: false, + }); + }); + + it('is selected properly', () => { + // This test should be passing by default. + // It sanity checks that the correct parser + // is being selected for URLs from this domain + const extractor = getExtractor(url); + assert.equal(extractor.domain, URL.parse(url).hostname); + }); + + it('returns the title', async () => { + // To pass this test, fill out the title selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + const { title } = await result; + + // Update these values with the expected values from + // the article. + assert.equal(title, `wasm-module - RustのWebAssemblyの中でDOMを扱う`); + }); + + it('returns the author', async () => { + // To pass this test, fill out the author selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + const { author } = await result; + + // Update these values with the expected values from + // the article. + assert.equal(author, null); + }); + + it('returns the date_published', async () => { + // To pass this test, fill out the date_published selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + const { date_published } = await result; + + // Update these values with the expected values from + // the article. + assert.equal(date_published, '2019-04-03T15:00:00.000Z'); + }); + + it('returns the dek', async () => { + // To pass this test, fill out the dek selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + const { dek } = await result; + + // Update these values with the expected values from + // the article. + assert.equal( + dek, + 'wasm-module - RustのWebAssemblyの中でDOMを扱うの使い方、日本語情報はMOONGIFTでチェック。個人的にWebAssemblyには強く期待しており、その中でもGoが有力だと感じています。それはGoのWebAssemblyではDOMやJavaScript APIが使えるからです。駆使すればWebアプリケーション全体のコードをGoで書けるのです。これがRustにもないのが残念でした。しかしwasm-moduleが新し...。MOONGIFTはオープンソース・ソフトウェアを毎日紹介' + ); + }); + + it('returns the lead_image_url', async () => { + // To pass this test, fill out the lead_image_url selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + const { lead_image_url } = await result; + + // Update these values with the expected values from + // the article. + assert.equal( + lead_image_url, + `https://s3-ap-northeast-1.amazonaws.com/moongift/attaches/16931/list.?1553229736` + ); + }); + + it('returns the content', async () => { + // To pass this test, fill out the content selector + // in ./src/extractors/custom/www.moongift.jp/index.js. + // You may also want to make use of the clean and transform + // options. + const { content } = await result; + + const $ = cheerio.load(content || ''); + + const first13 = excerptContent( + $('*') + .first() + .text(), + 1 + ); + + // Update these values with the expected values from + // the article. + assert.equal( + first13, + '個人的にWebAssemblyには強く期待しており、その中でもGoが有力だと感じています。それはGoのWebAssemblyではDOMやJavaScript' + ); + }); + }); +});