Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.2 KiB

  1. /*!
  2. * parse-github-url <https://github.com/jonschlinkert/parse-github-url>
  3. *
  4. * Copyright (c) 2015-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var url = require('url');
  9. var cache = {};
  10. module.exports = function parseGithubUrl(str) {
  11. return cache[str] || (cache[str] = parse(str));
  12. };
  13. function parse(str) {
  14. if (typeof str !== 'string' || !str.length) {
  15. return null;
  16. }
  17. if (str.indexOf('git@gist') !== -1 || str.indexOf('//gist') !== -1) {
  18. return null;
  19. }
  20. // parse the URL
  21. var obj = url.parse(str);
  22. if (typeof obj.path !== 'string' || !obj.path.length || typeof obj.pathname !== 'string' || !obj.pathname.length) {
  23. return null;
  24. }
  25. if (!obj.host && /^git@/.test(str) === true) {
  26. // return the correct host for git@ URLs
  27. obj.host = url.parse('http://' + str).host;
  28. }
  29. obj.path = trimSlash(obj.path);
  30. obj.pathname = trimSlash(obj.pathname);
  31. obj.filepath = null;
  32. if (obj.path.indexOf('repos') === 0) {
  33. obj.path = obj.path.slice(6);
  34. }
  35. var seg = obj.path.split('/').filter(Boolean);
  36. var hasBlob = seg[2] === 'blob';
  37. if (hasBlob && !isChecksum(seg[3])) {
  38. obj.branch = seg[3];
  39. if (seg.length > 4) {
  40. obj.filepath = seg.slice(4).join('/');
  41. }
  42. }
  43. var blob = str.indexOf('blob');
  44. if (blob !== -1) {
  45. obj.blob = str.slice(blob + 5);
  46. }
  47. var tree = str.indexOf('tree');
  48. if (tree !== -1) {
  49. var idx = tree + 5;
  50. var branch = str.slice(idx);
  51. var slash = branch.indexOf('/');
  52. if (slash !== -1) {
  53. branch = branch.slice(0, slash);
  54. }
  55. obj.branch = branch;
  56. }
  57. obj.owner = owner(seg[0]);
  58. obj.name = name(seg[1]);
  59. if (seg.length > 1 && obj.owner && obj.name) {
  60. obj.repo = obj.owner + '/' + obj.name;
  61. } else {
  62. var href = obj.href.split(':');
  63. if (href.length === 2 && obj.href.indexOf('//') === -1) {
  64. obj.repo = obj.repo || href[href.length - 1];
  65. var repoSegments = obj.repo.split('/');
  66. obj.owner = repoSegments[0];
  67. obj.name = repoSegments[1];
  68. } else {
  69. var match = obj.href.match(/\/([^\/]*)$/);
  70. obj.owner = match ? match[1] : null;
  71. obj.repo = null;
  72. }
  73. if (obj.repo && (!obj.owner || !obj.name)) {
  74. var segs = obj.repo.split('/');
  75. if (segs.length === 2) {
  76. obj.owner = segs[0];
  77. obj.name = segs[1];
  78. }
  79. }
  80. }
  81. if (!obj.branch) {
  82. obj.branch = seg[2] || getBranch(obj.path, obj);
  83. if (seg.length > 3) {
  84. obj.filepath = seg.slice(3).join('/');
  85. }
  86. }
  87. obj.host = obj.host || 'github.com';
  88. obj.owner = obj.owner || null;
  89. obj.name = obj.name || null;
  90. obj.repository = obj.repo;
  91. return obj;
  92. }
  93. function isChecksum(str) {
  94. return /^[a-f0-9]{40}$/i.test(str);
  95. }
  96. function getBranch(str, obj) {
  97. var segs = str.split('#');
  98. var branch;
  99. if (segs.length > 1) {
  100. branch = segs[segs.length - 1];
  101. }
  102. if (!branch && obj.hash && obj.hash.charAt(0) === '#') {
  103. branch = obj.hash.slice(1);
  104. }
  105. return branch || 'master';
  106. }
  107. function trimSlash(path) {
  108. return path.charAt(0) === '/' ? path.slice(1) : path;
  109. }
  110. function name(str) {
  111. return str ? str.replace(/^\W+|\.git$/g, '') : null;
  112. }
  113. function owner(str) {
  114. if (!str) return null;
  115. var idx = str.indexOf(':');
  116. if (idx > -1) {
  117. return str.slice(idx + 1);
  118. }
  119. return str;
  120. }