Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/232391

Change subject: WIP: Autolink typed ISBN/RFC/PMIDs
......................................................................

WIP: Autolink typed ISBN/RFC/PMIDs

Depends on Ibdad2fa98fca08eeaa96bf33a08dd7723c1edb8c in Parsoid.

Depends on I3dcd289ed7b565b9162ee671038eeb45449e1215 in ve-core.

Bug: T109498
Change-Id: I5650410d7fca30c90baddd4f0c3f6d80e6b39042
---
M modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js
M modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
2 files changed, 67 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/91/232391/1

diff --git a/modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js 
b/modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js
index 7de1f5e..e336ce8 100644
--- a/modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js
+++ b/modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js
@@ -45,11 +45,25 @@
                                method: 'autolinkUrl',
                                expectedRange: new ve.Range( 52, 52 ),
                                expectedData: function ( data ) {
-                                       for ( var i = 1; i < 51; i++ ) {
+                                       var i;
+                                       for ( i = 1; i < 51; i++ ) {
                                                data[i] = [ data[i], [ 0 ] ];
                                        }
                                },
                                msg: 'Strip trailing punctuation (but not 
matched parens)'
+                       },
+                       {
+                               html: '<p>RFC 1234 xyz</p>',
+                               range: new ve.Range( 1, 10 ),
+                               method: 'autolinkMagicLink',
+                               expectedRange: new ve.Range( 10, 10 ),
+                               expectedData: function ( data ) {
+                                       var i;
+                                       for ( i = 1; i < 9; i++ ) {
+                                               data[i] = [ data[i], [ 0 ] ];
+                                       }
+                               },
+                               msg: 'Autolink valid RFC'
                        }
                ];
 
diff --git a/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js 
b/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
index c9fcd71..493089d 100644
--- a/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
+++ b/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
@@ -31,7 +31,7 @@
  * @static
  * @property
  */
-ve.ui.MWLinkAction.static.methods = 
ve.ui.MWLinkAction.super.static.methods.concat( [ 'open' ] );
+ve.ui.MWLinkAction.static.methods = 
ve.ui.MWLinkAction.super.static.methods.concat( [ 'open', 'autolinkMagicLink' ] 
);
 
 /* Methods */
 
@@ -52,14 +52,27 @@
  * @inheritdoc
  * @return {ve.dm.MWExternalLinkAnnotation} The annotation to use.
  */
-ve.ui.MWLinkAction.prototype.getLinkAnnotation = function ( href ) {
-       var title,
+ve.ui.MWLinkAction.prototype.getLinkAnnotation = function ( linktext ) {
+       var title, targetData, m,
+               href = linktext;
+       // The link has been validated in #autolinkMagicLink and/or
+       // #autolinkUrl, so we can use a quick and dirty regexp here to pull
+       // apart the magic link.
+       m = /^(RFC|PMID|ISBN)\s+(\S.*)$/.exec( linktext );
+       if ( m && m[ 1 ] === 'RFC' ) {
+               href = '//tools.ietf.org/html/rfc' + m[2];
+       } else if ( m && m[ 1 ] === 'PMID' ) {
+               href = '//www.ncbi.nlm.nih.gov/pubmed/' + m[2] + 
'?dopt=Abstract';
+       } else if ( m && m[ 1 ] === 'ISBN' ) {
+               title = mw.Title.newFromText( 'Special:BookSources/' + 
m[2].replace( /[^0-9Xx]/g, '' ) );
+       } else {
                targetData = 
ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref(
                        href,
                        this.surface.getModel().getDocument().getHtmlDocument()
                );
-       if ( targetData.isInternal ) {
-               title = mw.Title.newFromText( targetData.title );
+               if ( targetData.isInternal ) {
+                       title = mw.Title.newFromText( targetData.title );
+               }
        }
        return title ?
                ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title ) :
@@ -67,6 +80,27 @@
                        type: 'link/mwExternal',
                        attributes: { href: href }
                } );
+};
+
+/**
+ * Autolink the selected RFC/PMID/ISBN, which may have trailing whitespace.
+ *
+ * @see ve.ui.LinkAction#autolinkUrl
+ * @method
+ * @return {boolean}
+ *   True if the selection is a valid RFC/PMID/ISBN and the autolink action
+ *   was executed; otherwise false.
+ */
+ve.ui.MWLinkAction.prototype.autolinkMagicLink = function () {
+       return this.autolink( function ( linktext ) {
+               if ( /^(RFC|PMID) [0-9]+$/.test( linktext ) ) {
+                       return true; // Valid RFC/PMID
+               }
+               if ( /^ISBN (97[89][- ]?)?([0-9][- ]?){9}[0-9Xx]$/.test( 
linktext ) ) {
+                       return true; // Valid ISBN
+               }
+               return false;
+       } );
 };
 
 /**
@@ -89,3 +123,16 @@
 /* Registration */
 
 ve.ui.actionFactory.register( ve.ui.MWLinkAction );
+
+ve.ui.commandRegistry.register(
+       new ve.ui.Command(
+               'autolinkMagicLink', ve.ui.MWLinkAction.static.name, 
'autolinkMagicLink',
+               { supportedSelections: [ 'linear' ] }
+       )
+);
+
+ve.ui.sequenceRegistry.register(
+       // This regexp doesn't have to be precise; we'll validate the magic
+       // link in #autolinkMagicLink above.
+       new ve.ui.Sequence( 'autolinkMagicLink', 'autolinkMagicLink', 
/\b(RFC|PMID|ISBN)\s+[0-9]([- 0-9]*[0-9Xx])?(\s|\n\n)$/, 0, true )
+);

-- 
To view, visit https://gerrit.wikimedia.org/r/232391
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5650410d7fca30c90baddd4f0c3f6d80e6b39042
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to