Giter Site home page Giter Site logo

bbcode-unit's Introduction

BBCode Unit Converter

Setup

Go to the creation form for custom BBCodes as per the instructions of https://www.phpbb.com/support/docs/en/3.1/kb/article/adding-custom-bbcodes-in-phpbb3/

In the first field, enter:

[unit]{REGEXP=/^[0-9a-zA-Z'"\.\s\x{00B0},]+$/u}[/unit]

In the second field, enter:

<span>{REGEXP}
<script>
document.write((function () {
function convert(t){var e;if(null!==(e=(t=(t=t.replace(/\s+/g,"")).replace(/,/g,"")).match(/^([0-9]+(\.[0-9]+)?)((c)?m)$/))){var r=parseFloat(e[1]),l=e[3];if("m"===l&&100<=r)return convert(r/1e3+"km");r=r*("m"===l?100:1);return(l=Math.floor(r/30.48))+"' "+(r=Math.round((r-30.48*l)/2.54))+'"'}if(null!==(e=t.match(/^([0-9]+)m([0-9]+)?$/)))return convert(e[1]+"."+(e[2]||0)+"m");if(null===(e=t.match(/^([0-9]+)'([0-9]+)"?$/)))return null!==(e=t.match(/^([0-9]+(\.[0-9]+)?)°?C$/))?(9*parseFloat(e[1])/5+32).toFixed(2)+"°F":null!==(e=t.match(/^([0-9]+(\.[0-9]+)?)°?F$/))?(5*(parseFloat(e[1])-32)/9).toFixed(2)+"°C":null!==(e=t.match(/^([0-9]+(\.[0-9]+)?)mi(\.|les)?$/))?(1.609344*parseFloat(e[1])).toFixed(2)+" km":null===(e=t.match(/^([0-9]+(\.[0-9]+)?)km$/))?null:(parseFloat(e[1])/1.609344).toFixed(2)+" miles";l=parseInt(e[1]),r=parseInt(e[2]);return Math.round(30.48*l+2.54*r)+" cm"}

var result = convert(`{REGEXP}`);
if (!result) { return ' (???)'; }
return ' (' + result + ')';
})())
</script>
</span>

You will get a warning due to the usage of a REGEXP instead of SIMPLETEXT so to be able to support the ', " and ° characters. As you can see above, the REGEXP in question is pretty strict, especially in the context of backquotes, and should offer no security concern.

Usage

Input:

[unit]1m 60[/unit]
[unit]1.85 m[/unit]
[unit]175cm[/unit]
[unit]5'6"[/unit]

[unit]1,136 miles[/unit]
[unit]7 mi[/unit]
[unit]241 km[/unit]
[unit]1500 m[/unit]

[unit]30°C[/unit]
[unit]80 F[/unit]

[unit]aaa[/unit]
[unit]` + alert('Hacker!'));(`[/unit]

Output:

1m 60 (5' 3")
1.85 m (6' 1")
175cm (5' 9")
5'6" (168 cm)

1,136 miles (1828.21 km)
7 mi (11.27 km)
241 km (149.75 miles)
1500 m (0.93 miles)

30°C (86.00°F)
80 F (26.67°C)

aaa (???)
[unit]` + alert('Hacker!'));(`[/unit]

Known limitations

  • Decimal separators other than the dot aren't supported. Commas will be interpreted as thousand separators.
  • Nothing will be converted on Internet Explorer.

In all cases, the original value will still be shown.

Technobabble

Run tests

yarn install
yarn test

Build

yarn install
yarn build

Remove the module.exports=convert; at the end manually.

bbcode-unit's People

Contributors

ceithir avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.