Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
str2tbl
<div class="container" id="app">
<div class="input-group-prepend">
<span class="input-group-text">.str file</span>
</div>
<div class="input-group">
<textarea v-model="strdata" class="form-control"></textarea>
</div>
<div class="btn-group m-4" role="group">
<button type="button" class="btn btn-primary" v-on:click="goClick">.str convert to .tbl</button>
</div>
<div class="input-group-prepend">
<span class="input-group-text">.tbl file</span>
</div>
<div class="input-group">
<textarea v-model="tbldata" class="form-control"></textarea>
</div>
</div>
function regexGet (regex, str) {
var m;
var matchs = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
matchs.push(m);
};
return matchs;
};
new Vue({
el: "#app",
data: {
strdata: ` **testing**
assi sele resid 1 .and. type HT3 end -
sele resid 2 .and. type HN end -
rmin 1.94190 rmax 5.94190 -
rexp @rexp fmax @fmax rswi @rswi sexp @sexp kmin @kmin kmax @kmax SUMR
assi sele resid 1 .and. type HT3 end -
sele resid 2 .and. type HA2 end -
rmin 2.58100 rmax 6.58100 -
rexp @rexp fmax @fmax rswi @rswi sexp @sexp kmin @kmin kmax @kmax SUMR`,
tbldata: ""
},
methods: {
goClick: function() {
this.tbldata = "";
var units = regexGet(/assi[\s\S]*?SUMR/gm, this.strdata);
units.forEach( (unit_match) => {
var id_data = regexGet(/resid\s*(\d+).*?type (\w+)/gm, unit_match);
var min_max = regexGet(/rmin\s*([\.\d]+) rmax\s*([\.\d]+)/gm, unit_match);
// possible error
if (!(min_max.length === 1 && min_max[0].length === 3 && id_data.length === 2 && id_data[0].length === 3 && id_data[1].length === 3))
console.log(unit_match);
min_max = [parseFloat(min_max[0][1]), parseFloat(min_max[0][2])];
// transfer to this format
// assign (resid 2 and name HA )(resid 18 and name HN ) 4.0 2.2 1.0
var resids = id_data.map( m =>
"(resid" + (" " + m[1]).slice(-5) + " and name " + (m[2] + " ").slice(0, 5) + ")");
this.tbldata += "assign " + resids.join('') + ' ' +
[(min_max[0] + min_max[1]) / 2, (min_max[1] - min_max[0]) / 2, 0].map(x => x.toPrecision(2)).join(' ') + '\n';
});
console.log(123);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
textarea {
min-height: 40vh;
font-family: monospace;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment