Skip to content

Commit

Permalink
Merge pull request #18 from p-l-/support-ivre-links
Browse files Browse the repository at this point in the history
Support parsing (again) links to IVRE namespace
  • Loading branch information
p-l- authored Jan 15, 2023
2 parents 9dfcbe6 + e8fb96d commit 04bf73b
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ enum ElementType {
HostName,
MacAddress,

IvreLink,
Unknown,
}

const MAC_ADDRESS =
/^[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}$/i;

function ivre_guess_type(element: string): ElementType {
function ivre_guess_type(element: string, base_directory: string): ElementType {
if (isIPAddress(element)) {
return ElementType.IpAddress;
}
Expand All @@ -143,6 +144,12 @@ function ivre_guess_type(element: string): ElementType {
if (MAC_ADDRESS.test(element)) {
return ElementType.MacAddress;
}
const ivre_link = new RegExp(
`^\\[\\[${base_directory}/(IP|Network|Hostname|MAC)/[^|\\]]+\\|[^\\]]+\\]\\]$`
);
if (ivre_link.test(element)) {
return ElementType.IvreLink;
}
return ElementType.Unknown;
}

Expand Down Expand Up @@ -707,7 +714,7 @@ function ivre_analyze_selection(
const links = [];
let content_data = editor.getSelection();
content_data.split(/\s+/).forEach((element) => {
switch (ivre_guess_type(element)) {
switch (ivre_guess_type(element, settings.base_directory)) {
case ElementType.IpAddress: {
new Notice(`Processing IP address: ${element}`);
const fname = ivre_handle_address(element, settings);
Expand Down Expand Up @@ -740,6 +747,39 @@ function ivre_analyze_selection(
}
break;
}
case ElementType.IvreLink: {
// This is already an IVRE link; just process the element again
// without adding it to `links`
const data = element.match(/\[\[[^|]+\|([^\]]+)\]\]/);
if (data !== null) {
switch (ivre_guess_type(data[1], settings.base_directory)) {
case ElementType.IpAddress: {
new Notice(`Processing IP address: ${data[1]}`);
ivre_handle_address(data[1], settings);
break;
}
case ElementType.IpNetwork: {
new Notice(`Processing network: ${data[1]}`);
ivre_handle_network(data[1], settings);
break;
}
case ElementType.HostName: {
new Notice(`Processing hostname: ${data[1]}`);
ivre_handle_hostname(data[1], settings);
break;
}
case ElementType.MacAddress: {
new Notice(`Processing MAC address: ${data[1]}`);
ivre_handle_mac(data[1], settings);
break;
}
default: {
new Notice(`Element ignored: ${data[1]}`);
}
}
}
break;
}
default: {
if (element) {
new Notice(`Element ignored: ${element}`);
Expand Down

0 comments on commit 04bf73b

Please sign in to comment.