Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Plugins ➜ Problems with escaping < > characters in plugins

Problems with escaping < > characters in plugins

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Eurotool   (14 posts)  Bio
Date Mon 19 Dec 2022 03:32 AM (UTC)
Message
Hello, I am attempting to use the Convert_Word plugin described here: http://www.gammon.com.au/forum/?id=8747&reply=4#reply4

My objective is to remove the "<Public> " statements that initiate chat lines in the public channel of a MUD.

Public = "",
on line 23 works fine, but does not remove the angle brackets.

I have tried the following, with no success:
<Public> = "",
-> Line 31: Elements terminated out of sequence, expected </Public>, got </script> (Cannot load)
\<Public\> = "",
-> Line 23: Attribute name must start with letter or underscore, but starts with "\" (Cannot load)
"<Public>" = "",
-> Line 31: Elements terminated out of sequence, expected </Public>, got </script> (Cannot load)
"\<Public\>" = "",
-> Line 23: Attribute name must start with letter or underscore, but starts with "\" (Cannot load)
&lt;Public&gt; = "",
-> [string "Plugin"]:2: unexpected symbol near '<'
\&lt\;Public\&gt\;
-> Line 31: No closing ";" in XML entity argument "&lt" (Cannot load)
-> [string "Plugin"]:2: unexpected symbol near '\'
\&lt;Public\&gt; = "",

<![CDATA[<Public>]]> = "",
-> [string "Plugin"]:2: unexpected symbol near '<'
<![CDATA[\<Public\>]]> = "",
-> [string "Plugin"]:2: unexpected symbol near '\'
<![CDATA[&lt;Public&gt;]]> = "",
-> [string "Plugin"]:2: unexpected symbol near '&'
<![CDATA[\&lt;Public\&gt;]]> = "",
-> [string "Plugin"]:2: unexpected symbol near '\'

I am out of options, not sure what to try next.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 19 Dec 2022 04:42 AM (UTC)

Amended on Mon 19 Dec 2022 04:44 AM (UTC) by Nick Gammon

Message

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Convert_Word"
   author="Nick Gammon"
   id="9c0ed228bdb4a71c77e85ce0"
   language="Lua"
   purpose="Converts one word to another"
   date_written="2008-06-29"
   requires="4.00"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>

conversions = {
  ["&lt;Public&gt;"] = "",
  foo = "bar",
  dog = "cat",
  horse = "donkey",

  -- add more here
  }
  
function OnPluginPacketReceived (s)
  return (string.gsub (s, "%a+", conversions))
end -- function OnPluginPacketReceived
</script>

</muclient>


Your problem is that in a Lua table, non-identifier type keys (like "<public>") have to be put into square brackets. And since it is a string you also quote it.

Then, since the plugin is in an XML file the < and > symbols have to be &lt; and &gt;.

Another approach, which saves you converting < and > symbols is to use CDATA, like this:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Convert_Word"
   author="Nick Gammon"
   id="9c0ed228bdb4a71c77e85ce0"
   language="Lua"
   purpose="Converts one word to another"
   date_written="2008-06-29"
   requires="4.00"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>

<![CDATA[

conversions = {
  ["<Public>"] = "",
  foo = "bar",
  dog = "cat",
  horse = "donkey",

  -- add more here
  }
  
function OnPluginPacketReceived (s)
  return (string.gsub (s, "%a+", conversions))
end -- function OnPluginPacketReceived

]]>

</script>

</muclient>

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 19 Dec 2022 04:49 AM (UTC)
Message
You tried a lot of stuff, you must almost have hit upon the correction solution yourself. :)

See here for more talk about tables: http://www.gammon.com.au/forum/?id=6036

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Eurotool   (14 posts)  Bio
Date Reply #3 on Thu 22 Dec 2022 10:02 PM (UTC)
Message
Unfortunately, nothing inside the brackets seem to be working.

["bar baz"] = "",

in both scripts does nothing when encountering the string -- so it's not just about the <>.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 22 Dec 2022 11:35 PM (UTC)

Amended on Thu 22 Dec 2022 11:37 PM (UTC) by Nick Gammon

Message
OK, the replacement function is this:


  return (string.gsub (s, "%a+", conversions))


That is searching for an alphabetic string. If you wanted other stuff in there, you would need to make a set, like this:


  return (string.gsub (s, "[%a<> ]+", conversions))


That will match one or more of letters, less-than signs, greater-than signs, or a space.

That's if the whole thing is inside CDATA, otherwise replace < with &lt; and > with &gt;

Or maybe:


  return (string.gsub (s, "<?[%a ]+>?", conversions))


That would constrain the matches to the < optionally being at the start, and the > optionally being at the end.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


7,075 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.