{"action":"created","issue":{"url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/412","repository_url":"https://api.github.com/repos/asciidoctor/asciidoctor.js","labels_url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/412/labels{/name}","comments_url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/412/comments","events_url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/412/events","html_url":"https://github.com/asciidoctor/asciidoctor.js/pull/412","id":290185195,"number":412,"title":"🚧 Resolves #399, allow class and class instance to be registered as extensions","user":{"login":"Mogztter","id":333276,"avatar_url":"https://avatars3.githubusercontent.com/u/333276?v=4","gravatar_id":"","url":"https://api.github.com/users/Mogztter","html_url":"https://github.com/Mogztter","followers_url":"https://api.github.com/users/Mogztter/followers","following_url":"https://api.github.com/users/Mogztter/following{/other_user}","gists_url":"https://api.github.com/users/Mogztter/gists{/gist_id}","starred_url":"https://api.github.com/users/Mogztter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mogztter/subscriptions","organizations_url":"https://api.github.com/users/Mogztter/orgs","repos_url":"https://api.github.com/users/Mogztter/repos","events_url":"https://api.github.com/users/Mogztter/events{/privacy}","received_events_url":"https://api.github.com/users/Mogztter/received_events","type":"User","site_admin":false},"labels":[{"id":366616785,"url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/labels/wip","name":"wip","color":"eeeeee","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2018-01-20T11:27:05Z","updated_at":"2018-03-13T10:49:26Z","closed_at":null,"author_association":"MEMBER","pull_request":{"url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/pulls/412","html_url":"https://github.com/asciidoctor/asciidoctor.js/pull/412","diff_url":"https://github.com/asciidoctor/asciidoctor.js/pull/412.diff","patch_url":"https://github.com/asciidoctor/asciidoctor.js/pull/412.patch"},"body":"I think it's fair to say that nobody will create a class using Opal syntax:\r\n\r\n```js\r\nvar superclass = Opal.const_get_qualified(Extensions, 'IncludeProcessor');\r\nvar scope = Opal.klass(Opal.Object, superclass, name, function () {});\r\nOpal.defn(scope, '$initialize', function initialize () {\r\n  Opal.send(this, Opal.find_super_dispatcher(this, 'initialize', initialize));\r\n});\r\nOpal.defn(scope, '$process', function (doc, reader, target, attrs) {\r\n  reader.pushInclude(['included content'], target, target, 1, attrs)\r\n}); \r\nreturn scope;\r\n```\r\n\r\nWe could create a low level API to declare a class and define method on this class or we could pass a list of methods to register:\r\n\r\n**Low level API**\r\n```js\r\nvar includeProcessorClass = asciidoctor.Class.create(Extensions, 'IncludeProcessor');\r\nasciidoctor.Class.define(includeProcessorClass, 'process', function (doc, reader, target, attrs) {\r\n  reader.pushInclude(['included content'], target, target, 1, attrs)\r\n});\r\n```\r\n\r\n**List of methods**\r\n```js\r\nExtensions.createIncludeProcessor = function (name, functions) {\r\n  // ...\r\n  for (var key in functions) {\r\n    if (functions.hasOwnProperty(key)) {\r\n      Opal.defn(scope, '$' + key, functions[key]);\r\n    }\r\n  }\r\n});\r\n```\r\n\r\n@mojavelinux Once we agreed, I will implement the remaining functions :wink: "},"comment":{"url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/comments/372624101","html_url":"https://github.com/asciidoctor/asciidoctor.js/pull/412#issuecomment-372624101","issue_url":"https://api.github.com/repos/asciidoctor/asciidoctor.js/issues/412","id":372624101,"user":{"login":"Mogztter","id":333276,"avatar_url":"https://avatars3.githubusercontent.com/u/333276?v=4","gravatar_id":"","url":"https://api.github.com/users/Mogztter","html_url":"https://github.com/Mogztter","followers_url":"https://api.github.com/users/Mogztter/followers","following_url":"https://api.github.com/users/Mogztter/following{/other_user}","gists_url":"https://api.github.com/users/Mogztter/gists{/gist_id}","starred_url":"https://api.github.com/users/Mogztter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mogztter/subscriptions","organizations_url":"https://api.github.com/users/Mogztter/orgs","repos_url":"https://api.github.com/users/Mogztter/repos","events_url":"https://api.github.com/users/Mogztter/events{/privacy}","received_events_url":"https://api.github.com/users/Mogztter/received_events","type":"User","site_admin":false},"created_at":"2018-03-13T10:49:26Z","updated_at":"2018-03-13T10:49:26Z","author_association":"MEMBER","body":"> For the second approach, we could allow an arbitrary object to be passed as the third argument. This would get stored in the instance variable \"state\" and could be accessed from the process method using this.state.\r\n\r\n`this` in the process method is not bound to include processor instance but to the \"User Context\" (global context ?).\r\nI've tried to use the `bind` function but without success: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind\r\n\r\n```js\r\nOpal.defn(scope, '$' + key, functions[key].bind(scope));\r\n```\r\n\r\nIn the following example, we can see that `this` is not bound to the include processor instance:\r\n```js\r\nconst $callback = Symbol('callback');\r\nlet includeProcessor = asciidoctor.Extensions.createIncludeProcessor('StaticIncludeProcessor', {\r\n  postConstruct: (context) => {\r\n    this[$callback] = (value => 'you should ' + value);\r\n  },\r\n  process: (doc, reader, target, attrs) => {\r\n    reader.pushInclude([this[$callback]('pass')], target, target, 1, attrs);\r\n  }\r\n});\r\nlet includeProcessorInstance = includeProcessor.$new();\r\nthis[$callback] = (value) => value; // ERROR: it will override the function defined in the postConstruct function\r\nregistry.includeProcessor(includeProcessorInstance);\r\n```\r\n\r\n"}}
{"actor":{"display_login":"Mogztter"}}