Code conversion method, equipment and storage medium

文档序号:1363070 发布日期:2020-08-11 浏览:18次 中文

阅读说明:本技术 一种代码转换方法、设备和存储介质 (Code conversion method, equipment and storage medium ) 是由 蔡亮 张帅 李伟 匡立中 邱炜伟 于 2020-04-09 设计创作,主要内容包括:本发明提供本发明提供一种代码转换方法、设备和存储介质,包括:将第一源代码中所有以双斜杠“//”开头,以换行结尾的代码删除后并生成第一文本;将第一文本中所有的空格字符删除后生成第二文本;判断第一文本中左右花括号数量是否相等:是,则顺序遍历第二文本,根据匹配的花括号类型执行对应的操作以生成节点树;根据节点树按照设定的层级、缩进规则生成第二代码。本发明根据不同的花括号类型做出不同的操作,以供构造节点树,并根据节点树按照相应的规则快速直接生成第二代码。(The invention provides a code conversion method, a device and a storage medium, comprising the following steps: deleting all codes beginning with a double slash '//' and ending with line feed in the first source code and generating a first text; deleting all space characters in the first text to generate a second text; judging whether the quantity of the left curly braces and the quantity of the right curly braces in the first text are equal: if yes, traversing the second text in sequence, and executing corresponding operation according to the matched curly brace type to generate a node tree; and generating a second code according to the node tree and the set hierarchy and indentation rule. The invention makes different operations according to different curly brace types so as to construct the node tree, and the second code is quickly and directly generated according to the node tree and the corresponding rule.)

1. A method of transcoding comprising

Deleting all codes beginning with a double slash '//' and ending with line feed in the first source code and generating a first text;

deleting all space characters in the first text to generate a second text;

judging whether the quantity of the left curly braces and the quantity of the right curly braces in the first text are equal:

if yes, traversing the second text in sequence, and executing corresponding operation according to the matched curly brace type to generate a node tree;

and generating a second code according to the node tree and a set hierarchy and indentation rule.

2. The method of claim 1, wherein deleting all codes in the first source code that begin with a double slash "//" and end with a line feed and generating the first text comprises:

identifying the beginning of all double slashes '/' in the first source code through a first regular expression/\\/- \/- \/\ n \/so as to change the code at the tail of the line, and generating a first text after deleting; wherein \/representative matches a left slash; represents an arbitrary character; represents the number of matches, 0 or n, i.e. represents any character of indeterminate number of matches; \ n represents a line feed character, i.e., the end of a line.

3. The method of claim 2, wherein generating a second text after deleting all space characters in the first text comprises:

identifying hollow lattice characters in the first text through a second regular expression/\ s +/and generating a second text after deleting; wherein, s represents matching a space character; + represents the number of matches, one or more matches, \ s + represents matching of consecutive spaces of arbitrary length.

4. The method of claim 3, wherein performing corresponding operations to generate a node tree according to the matched curly brace type comprises:

configuring a node to be pressed into the stack bottom; the root node comprises a node type, a node name, a node value and a child node list;

judging the type of the matched curly braces:

if the type is left curly brackets, generating a first node and then pressing the first node into a stack;

if the type is right curly brace, pushing out the current node in the stack to be used as the child node of the next node, and putting the child node into a child node list of the next node;

and traversing the second text until all curly brackets are traversed.

5. The method of claim 4, wherein generating the second code according to the node tree and the set hierarchy and indentation rule comprises:

acquiring information of a root node of a node tree, and creating a html father label according to the node type, the node name and the node value;

generating a plurality of html sub-labels corresponding to the information of the plurality of child nodes of the child node list, and sequentially placing the html sub-labels in the parent label;

and according to each level, filling the html tags according to a set indentation rule to generate a second code, namely an html code.

6. The method according to any of claims 1-5, wherein the first source code is a Less code and/or a Scss code.

7. An apparatus, characterized in that the apparatus comprises:

one or more processors;

a memory for storing one or more programs,

the one or more programs, when executed by the one or more processors, cause the one or more processors to perform the method recited in any of claims 1-6.

8. A storage medium storing a computer program, characterized in that the program, when executed by a processor, implements the method according to any one of claims 1-6.

Technical Field

The present application relates to the field of computer technologies, and in particular, to a method, an apparatus, and a storage medium for transcoding.

Background

In the front-end domain, when standard CSS is used, styles are defined for multi-level nested elements, either using the nesting definition of the descendant selector from outside to inside, or adding a class name or id to this element. Such writing, while simple, is inconvenient to maintain because of the dispersed CSS syntax and the inability to clearly understand the hierarchy and associations between styles. Therefore, a Less language was born, which is a CSS preprocessor that allows you to define styles using nested grammars. In Less, the nesting rule makes the CSS syntax decentralized, and the hierarchy is unclear. Nesting rules allow one selector to be nested in another selector, so that compact code can be designed more easily, the relationship between styles is clear, the corresponding relationship between a Less level and an html level in corresponding html code is more clear, and the positioning can be faster when the code is maintained and modified.

However, when writing CSS codes or Less codes, corresponding html codes need to be written manually, and the corresponding html codes can be automatically generated by using Less code rules, so that the workload is reduced. But at present, no method for converting the Less code into the corresponding html code exists.

Disclosure of Invention

In view of the above-mentioned drawbacks and deficiencies of the prior art, it would be desirable to provide a simple and efficient transcoding method, apparatus and storage medium.

In a first aspect, the present invention provides a transcoding method, comprising:

deleting all codes beginning with a double slash '//' and ending with line feed in the first source code and generating a first text;

deleting all space characters in the first text to generate a second text;

judging whether the quantity of the left curly braces and the quantity of the right curly braces in the first text are equal:

if yes, traversing the second text in sequence, and executing corresponding operation according to the matched curly brace type to generate a node tree;

and generating a second code according to the node tree and the set hierarchy and indentation rule.

Further, deleting all codes beginning with a double slash "///" and ending with a line feed in the first source code and generating a first text, wherein the steps comprise:

identifying the beginning of all double slashes '/' in the first source code through a first regular expression/\\/- \/-n/, so as to change the code at the tail of the line, and generating a first text after deleting; wherein \/representative matches a left slash; represents an arbitrary character; represents the number of matches, 0 or n, i.e. represents any character of indeterminate number of matches; \ n represents a line feed character, i.e., the end of a line.

Further, generating a second text after deleting all space characters in the first text, including:

identifying hollow lattice characters in the first text through a second regular expression/\ s +/and generating a second text after deleting; wherein, s represents matching a space character; + represents the matching times, and matching once or more times, then \ s + represents matching continuous spaces of any length.

Further, according to the matched curly brace type, executing corresponding operation to generate a node tree, including:

configuring a node to be pressed into the stack bottom; the root node comprises a node type, a node name, a node value and a child node list;

judging the type of the matched curly braces:

if the type is left curly brackets, generating a first node and then pressing the first node into a stack;

if the type is right curly brace, pushing out the current node in the stack to be used as the child node of the next node, and putting the child node into a child node list of the next node;

the second text is traversed until all curly braces have been traversed.

Further, generating a second code according to the node tree and the set hierarchy and indentation rule, wherein the generating of the second code comprises the following steps:

acquiring information of a root node of a node tree, and creating a html father label according to the node type, the node name and the node value;

generating a plurality of html sub-labels corresponding to the information of the plurality of child nodes of the child node list, and sequentially placing the html sub-labels in the parent label;

and according to each level, filling the html tags according to a set indentation rule to generate a second code, namely an html code.

Further, the first source code is a Less code and/or a Scas code.

In a second aspect, the present invention also provides an apparatus comprising one or more processors and a memory, wherein the memory contains instructions executable by the one or more processors to cause the one or more processors to perform a transcoding method provided according to embodiments of the present invention.

In a third aspect, the present invention also provides a storage medium storing a computer program that causes a computer to execute the transcoding method provided according to the embodiments of the present invention.

According to the code conversion method, the code conversion device and the code conversion storage medium provided by the embodiments of the invention, after the first source code is converted into the code text without the space character, different operations are performed according to different curly bracket types so as to construct the node tree, and the second code is rapidly and directly generated according to the corresponding rule according to the node tree.

Drawings

Other features, objects and advantages of the present application will become more apparent upon reading of the following detailed description of non-limiting embodiments thereof, made with reference to the accompanying drawings in which:

fig. 1 is a flowchart of a transcoding method according to an embodiment of the present invention.

Fig. 2 is a diagram of a node tree structure generated in accordance with a preferred embodiment shown in fig. 1.

Fig. 3 is a schematic view of a scenario of step 13 in a preferred embodiment of the method shown in fig. 1.

Fig. 4 is a schematic structural diagram of an apparatus according to an embodiment of the present invention.

Detailed Description

The present application will be described in further detail with reference to the following drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the relevant invention and not restrictive of the invention. It should be noted that, for convenience of description, only the portions related to the present invention are shown in the drawings.

It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.

Fig. 1 is a flowchart of a transcoding method according to an embodiment of the present invention.

As shown in fig. 1, in the present embodiment, the present invention provides a transcoding method, including:

s11, deleting all codes beginning with a parallel slash '//' and ending with line feed in the first source code to generate a first text;

s12, deleting all space characters in the first text to generate a second text;

s13, judging whether the number of left and right curly braces in the first text is equal:

if yes, traversing the second text in sequence, and executing corresponding operation according to the matched curly brace type to generate a node tree;

and S14, generating a second code according to the set hierarchy and indentation rule according to the node tree. Specifically, the following less source code is taken as an example:

deleting all codes beginning with a double slash '//' and ending with line feed in the first source code and generating a first text; the following were used:

deleting all space characters in the first text to generate a second text; the following were used: common-select-wrap { position: relative; common-select-search { cursor: pointer; font-size of 16 px; position, absolute; top is 50 percent; margin-top: -8 px; }. common-select { cursor: initial; p { height:32 px; line-height:31 px; font-size:14 px; }}

As shown in the second text, if the number of left curly braces (4) is the same as the number of right curly braces (4), the second text is sequentially traversed, and corresponding operations are performed according to the matched curly brace types to generate a node tree, as shown in fig. 2.

And finally, generating a second code according to the node tree and the set hierarchy and indentation rule, namely:

it should be noted that the first language code in the method is suitable for most program languages, such as JavaScript, less, css, and scs, and the like, and according to the difference of the attributes of specific languages, some languages cannot well realize the required functions after being converted into html language, and the method is a better choice for the less language and the scs language.

Further preferably, the first text is generated after all codes in the first source code that start with a double slash "//" and end with a line feed are deleted, including:

identifying all the double slashes '/' beginning in the first source code and the code ending in the line feed through a first regular expression/\/- \ n/, and generating a first text after deleting; wherein \/representative matches a left slash; represents an arbitrary character; represents the number of matches, 0 or n, i.e. represents any character of indeterminate number of matches; \ n represents a line feed character, i.e., the end of a line.

And matching all the codes in the first source code beginning with a parallel slash '//', and replacing codes at the end of line feed to generate the first text through the first regular expression.

Further preferably, the step S11 is to delete all space characters in the first text to generate the second text, and includes:

identifying hollow lattice characters in the first text through a second regular expression/\ s +/and generating a second text after deleting; wherein, s represents matching a space character; + represents the number of matches, one or more matches, \ s + represents matching of consecutive spaces of arbitrary length. And generating a second text without space characters through the regular expression to prepare for subsequent matching judgment.

In further embodiments, the step S13 performs corresponding operations according to the matched curly brace type to generate a node tree, including:

s131, configuring a node to be pressed into the stack bottom; the root node comprises a node type, a node name, a node value and a child node list;

s132, judging the type of the matched curly braces:

if the type is left curly brackets, generating a first node and then pressing the first node into a stack;

if the type is right curly brace, pushing out the current node in the stack to be used as the child node of the next node, and putting the child node into a child node list of the next node;

the second text is traversed until all curly braces have been traversed.

Specifically, taking the second text as an example, first, a root node is configured, such as:

type:'id',

value:'root',

nodeName:'div',

child:[]

the root node information may include: node type, node name, node value, child list child [ ], and pushes the object node onto the stack.

In a preferred embodiment of the present invention,

the judgment basis of the node type can be as follows: 1. if the matched text begins with the 'text', the node type is a default configuration item (the configuration item is generally set to class or className) acquired from the environment; 2. if the matched text starts with a "#" character, the node type is id; 3. if the matched text is at the beginning of the text, the node type is set as reverse; 4. if none of the above rules apply, then set the node type to tag).

The configuration method of the node value comprises the following steps: the content before the left curly brace that is matched is removed and the # & character is removed, e.g., the common-select-wrap {, then the node value is common-select-wrap).

The configuration method of the node name comprises the following steps: 1. judging whether the node type is tag or not, if so, determining that the node value is the content in front of the left curly brace, such as p {, and the node name is p; 2. if the node type is not tag, then the node name is the default configuration item retrieved from the environment (typically set to div)) and a regular expression is used to match the right curly brace.

As shown in fig. 3, the first curly brace traversing the second text is a left curly brace, at this time, a node a is generated and pushed into the stack, the second text is continuously traversed, and the next curly brace matched is still a left curly brace, a node B is generated and pushed into the stack; and continuously traversing the second text, wherein the next matched brace is a right brace, at this time, the node B is pushed out from the stack, after the node B is pushed out, the latest node is the node A, the node B is added into the child node list of the node A, the second text is continuously traversed, the next matched brace is still a left brace, the node C is generated and is pressed into the stack … …, until the last matched brace is the right brace, the node A (the child nodes are B, C and D) is added into the child node list of the root node root, and finally the node tree shown in the figure 2 is generated.

In a preferred embodiment, the matching expression is:

v ([ # ] [ - \\ w: @, ] + [ { ]) | (&: wherein the regular expression has four branch conditions, each wrapped with () and split with |

1. [. # ] represents a match once character or a once # character;

[ - \ w () @, ] + represents one or more times of matching-, \ w, (,), @, the character

Great face for matching

2. The & represents the beginning with the & character,

? Representing as few as possible matching arbitrary characters

Great face for matching

3. [ a-z ] + identification matches any lower case letter one or more times

Great face for matching

4. { represents the matching primary opening

In further embodiments, the generating the second code according to the node tree and the set hierarchy and indentation rule in step S14 includes:

s141, acquiring information of a root node of a node tree, and creating a html father label according to a node type, a node name and a node value;

s142, generating a plurality of corresponding htmlL sub-labels according to the information of the plurality of child nodes in the child node list, and sequentially placing the htmlL sub-labels in the father label;

and S143, according to each hierarchy, filling the html tags according to the set indentation rule to generate a second code, namely an html code.

Specifically, after the filter node type is reverse, the tree is traversed recursively, during traversal, each node generates an html node, then each node generates a corresponding indentation according to a corresponding level, recursive traversal refers to traversal of all child nodes of a node, and if the child nodes are still child nodes, the child nodes of the child nodes are traversed again until the child nodes are empty. The generation rule is as follows

1.`\n${″.repeat(level*4)}<${nodeName}${type!=='tag'?`${type}='${value}'`:″}>\n${″.repeat(level*6)}\n${″.repeat(level*4)}</${nodeName}>`;

Wherein:

repeat (level 4) } indicates that a line feed is inserted in front of each line, and then a space with 4 times of the corresponding level is inserted, wherein the level is the level in the recursive traversal, and the number of the spaces is 4 more when the level is increased by one;

2. the following rules generate the start tag of the node, and the place-occupying variable of $ nodeName is replaced by the variable in the node;

<${nodeName}${type!=='tag'?`${type}='${value}'`:″}>

note that $ { type! Is? The expression "$ { type } $, $ { value }'": } means that if the node type is tag, nothing is filled in the tag, and if the node type is not tag, the node type is filled in the tag — the node value;

the above rules apply for the following nodes as examples:

the result is generated as

<div class='common-select-search'>

Using the following nodes as an example, the above rule is applied,

generating results

<p>

3. The following rules indicate that filling a new line first, then filling the corresponding level 6 times spaces, refilling a new line, filling the corresponding level 4 times spaces

\n${″.repeat(level*6)}\n${″.repeat(level*4)}

4. The following rules generate end tags for nodes

</${nodeName}>`;

Generating html by each node according to the rule, and obtaining the following results:

fig. 4 is a schematic structural diagram of an apparatus according to an embodiment of the present invention.

As shown in fig. 4, as another aspect, the present application also provides an apparatus 400 including one or more Central Processing Units (CPUs) 401 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)402 or a program loaded from a storage section 408 into a Random Access Memory (RAM) 403. In the RAM403, various programs and data necessary for the operation of the device 400 are also stored. The CPU401, ROM402, and RAM403 are connected to each other via a bus 404. An input/output (I/O) interface 405 is also connected to bus 404.

The following components are connected to the I/O interface 405: an input section 406 including a keyboard, a mouse, and the like; an output section 407 including a display device such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage section 408 including a hard disk and the like; and a communication section 409 including a network interface card such as a LAN card, a modem, or the like. The communication section 409 performs communication processing via a network such as the internet. A driver 410 is also connected to the I/O interface 405 as needed. A removable medium 411 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 410 as necessary, so that a computer program read out therefrom is mounted into the storage section 408 as necessary.

In particular, according to an embodiment of the present disclosure, the transcoding method described in any of the above embodiments may be implemented as a computer software program. For example, embodiments of the present disclosure include a computer program product comprising a computer program tangibly embodied on a machine-readable medium, the computer program comprising program code for performing a transcoding method. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 409, and/or installed from the removable medium 411.

As yet another aspect, the present application also provides a computer-readable storage medium, which may be the computer-readable storage medium included in the apparatus of the above-described embodiment; or it may be a separate computer readable storage medium not incorporated into the device. The computer readable storage medium stores one or more programs for use by one or more processors in performing the transcoding method described herein.

The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.

The units or modules described in the embodiments of the present application may be implemented by software or hardware. The described units or modules may also be provided in a processor, for example, each of the described units may be a software program provided in a computer or a mobile intelligent device, or may be a separately configured hardware device. Wherein the designation of a unit or module does not in some way constitute a limitation of the unit or module itself.

The above description is only a preferred embodiment of the application and is illustrative of the principles of the technology employed. It will be appreciated by those skilled in the art that the scope of the invention herein disclosed is not limited to the particular combination of features described above, but also encompasses other arrangements formed by any combination of the above features or their equivalents without departing from the spirit of the present application. For example, the above features may be replaced with (but not limited to) features having similar functions disclosed in the present application.

14页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种低空雷达多席位空域数据编辑方法

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!